Decrypt a String Using an AES Key
Create a NAE Session. Pass NAE username, password, and property file path as arguments.
NaeSession session = new NaeSession(user, passwd, propFilePath);
Create an
NaeRijndaelKey
object. Pass theNaeSession
object andkeyname
as arguments.NaeRijndaelKey key = new NaeRijndaelKey(session, keyname);
Specify an IV, if necessary, by creating a new byte array.
byte[] iv = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35 }; key.IV = iv;
Set key mode and padding.
key.Padding = PaddingMode.PKCS7; key.Mode = CipherMode.CBC;
Decrypt the data.
ICryptoTransform decryptor = key.CreateDecryptor(); System.IO.MemoryStream memstr2 = new System.IO.MemoryStream(); CryptoStream decrstr = new CryptoStream(memstr2, key.CreateDecryptor(), CryptoStreamMode.Write); decrstr.Write(encrBytes, 0, encrBytes.Length); decrstr.Close(); byte[] decrBytes = memstr2.ToArray(); Console.WriteLine("Decrypted {0} bytes: {1}", Convert.ToString(decrBytes.Length), new String(Encoding.UTF8.GetChars(decrBytes)));
Note
The CreateDecryptor
API doesn't support decrypting large data using chunking mechanism. To decrypt large data, refer to Encrypting/Decrypting a File.