I have the following code for encryption and decryption. The problem is that at decryption besides the decrypted text i have some "aaaaa" after the text. why? need some help. THX!
public static byte[] Encrypt(byte[] PlainTextBytes, string key , string InitialVector) { try { System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); Byte[] KeyBytes = encoding.GetBytes(key); byte[] InitialVectorBytes = encoding.GetBytes(InitialVector); RijndaelManaged SymmetricKey = new RijndaelManaged(); ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes); MemoryStream MemStream = new MemoryStream(); CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write); CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length); CryptoStream.FlushFinalBlock(); byte[] CipherTextBytes = MemStream.ToArray(); return CipherTextBytes; //decrytion
public static string Decrypt(byte[] PlainTextBytes1, string key, string InitialVector) { System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); Byte[] KeyBytes = encoding.GetBytes(key); RijndaelManaged SymmetricKey = new RijndaelManaged(); byte[] InitialVectorBytes = Encoding.UTF8.GetBytes(InitialVector); ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes); MemoryStream MemStream1 = new MemoryStream(PlainTextBytes1); CryptoStream CryptoStream = new CryptoStream(MemStream1, Decryptor, CryptoStreamMode.Read); Byte[] pltxt = new byte[PlainTextBytes1.Length]; CryptoStream.Read(pltxt, 0, pltxt.Length); ASCIIEncoding textConverter = new ASCIIEncoding(); round = textConverter.GetString(pltxt); return round; } where am i wrong?
byte[]the same sizes?