1.带初始化向量
public class AesEncryption
{ private readonly byte [ ] aesKey = { 0x11 , 0x22 , 0x33 , 0x44 , 0x55 , 0x66 , 0x77 , 0x88 , 0x99 , 0x00 , 0x11 , 0x22 , 0x33 , 0x44 , 0x55 } ; private readonly string key = "0123456789abcdef" ; private readonly string iv = "0000000000000000" ; public string Encrypt ( string plainText) { using ( Aes aesAlg = Aes. Create ( ) ) { aesAlg. Key = Encoding. UTF8. GetBytes ( key) ; aesAlg. IV = Encoding. UTF8. GetBytes ( iv) ; ICryptoTransform encryptor = aesAlg. CreateEncryptor ( aesAlg. Key, aesAlg. IV) ; using ( MemoryStream msEncrypt = new MemoryStream ( ) ) { using ( CryptoStream csEncrypt = new CryptoStream ( msEncrypt, encryptor, CryptoStreamMode. Write) ) using ( StreamWriter swEncrypt = new StreamWriter ( csEncrypt) ) { swEncrypt. Write ( plainText) ; } byte [ ] encrypted = msEncrypt. ToArray ( ) ; return Convert. ToBase64String ( encrypted) ; } } } public string Decrypt ( string cipherText) { using ( Aes aesAlg = Aes. Create ( ) ) { aesAlg. Key = Encoding. UTF8. GetBytes ( key) ; aesAlg. IV = Encoding. UTF8. GetBytes ( iv) ; ICryptoTransform decryptor = aesAlg. CreateDecryptor ( aesAlg. Key, aesAlg. IV) ; using ( MemoryStream msDecrypt = new MemoryStream ( Convert. FromBase64String ( cipherText) ) ) using ( CryptoStream csDecrypt = new CryptoStream ( msDecrypt, decryptor, CryptoStreamMode. Read) ) using ( StreamReader srDecrypt = new StreamReader ( csDecrypt) ) { return srDecrypt. ReadToEnd ( ) ; } } }
}
private void test_aes ( )
{ string original = "This is the data to be encrypted." ; AesEncryption aes = new AesEncryption ( ) ; { string encrypted = aes. Encrypt ( original) ; Console. WriteLine ( $"Encrypted: { encrypted } " ) ; string decrypted = aes. Decrypt ( encrypted) ; Console. WriteLine ( $"Decrypted: { decrypted } " ) ; }
}
2.不带初始化向量, ECB, 128bit, base64
public class AESNoIVExample
{ public byte [ ] GenerateRandomSalt ( ) { byte [ ] salt = new byte [ 16 ] ; using ( var rng = new RNGCryptoServiceProvider ( ) ) { rng. GetBytes ( salt) ; } return salt; } public byte [ ] Encrypt ( string plainText, byte [ ] Key) { if ( plainText == null || plainText. Length <= 0 ) throw new ArgumentNullException ( nameof ( plainText) ) ; if ( Key == null || Key. Length <= 0 ) throw new ArgumentNullException ( nameof ( Key) ) ; byte [ ] encrypted; using ( Aes aesAlg = Aes. Create ( ) ) { aesAlg. Key = Key; aesAlg. Mode = CipherMode. ECB; aesAlg. Padding = PaddingMode. PKCS7; ICryptoTransform encryptor = aesAlg. CreateEncryptor ( aesAlg. Key, null ) ; using ( MemoryStream msEncrypt = new MemoryStream ( ) ) { using ( CryptoStream csEncrypt = new CryptoStream ( msEncrypt, encryptor, CryptoStreamMode. Write) ) using ( StreamWriter swEncrypt = new StreamWriter ( csEncrypt) ) { swEncrypt. Write ( plainText) ; } encrypted = msEncrypt. ToArray ( ) ; } } return encrypted; } public string Decrypt ( byte [ ] cipherText, byte [ ] Key) { if ( cipherText == null || cipherText. Length <= 0 ) throw new ArgumentNullException ( nameof ( cipherText) ) ; if ( Key == null || Key. Length <= 0 ) throw new ArgumentNullException ( nameof ( Key) ) ; string plaintext = null ; using ( Aes aesAlg = Aes. Create ( ) ) { aesAlg. Key = Key; aesAlg. Mode = CipherMode. ECB; aesAlg. Padding = PaddingMode. PKCS7; ICryptoTransform decryptor = aesAlg. CreateDecryptor ( aesAlg. Key, null ) ; using ( MemoryStream msDecrypt = new MemoryStream ( cipherText) ) { using ( CryptoStream csDecrypt = new CryptoStream ( msDecrypt, decryptor, CryptoStreamMode. Read) ) using ( StreamReader srDecrypt = new StreamReader ( csDecrypt) ) { plaintext = srDecrypt. ReadToEnd ( ) ; } } } return plaintext; } }
private void test_aes ( )
{ string plaintext = "This is the data to be encrypted." ; string password = "0123456789abcdef" ; AESNoIVExample ase2 = new AESNoIVExample ( ) ; byte [ ] key = Encoding. UTF8. GetBytes ( password) ; byte [ ] encrypted2 = ase2. Encrypt ( plaintext, key) ; string base64Encrypted = Convert. ToBase64String ( encrypted2) ; Console. WriteLine ( "Encrypted (Base64): " + base64Encrypted) ; byte [ ] decryptedBytes = Convert. FromBase64String ( base64Encrypted) ; string decrypted2 = ase2. Decrypt ( decryptedBytes, key) ; Console. WriteLine ( "Decrypted: " + decrypted2) ;
}