Premal

Premal

  • NA
  • 1
  • 0

AES Decryption problem

Jul 20 2007 4:10 AM
Here is the code: public static string Encrypt(string plainString, string password, string saltString) { byte[] plainBytes = Encoding.Unicode.GetBytes(plainString); byte[] saltBytes = Encoding.ASCII.GetBytes(saltString); PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(password, saltBytes); RijndaelManaged Cipher = new RijndaelManaged(); ICryptoTransform Encryptor = Cipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16)); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, Encryptor, CryptoStreamMode.Write); cStream.Write(plainBytes, 0, plainBytes.Length); cStream.FlushFinalBlock(); byte[] captchaResult = mStream.ToArray(); mStream.Close(); cStream.Close(); return Convert.ToBase64String(captchaResult); } public static string Decrypt(string cipherString, string password, string saltString) { byte[] cipherBytes = Encoding.UTF8.GetBytes(cipherString); byte[] saltBytes = Encoding.ASCII.GetBytes(saltString); PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(password, saltBytes); RijndaelManaged Cipher = new RijndaelManaged(); ICryptoTransform Decryptor = Cipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16)); MemoryStream mStream = new MemoryStream(cipherBytes); CryptoStream cStream = new CryptoStream(mStream, Decryptor, CryptoStreamMode.Read); byte[] plainBytes = new byte[cipherBytes.Length]; int decrCount = cStream.Read(plainBytes, 0, plainBytes.Length); mStream.Close(); cStream.Close(); return Encoding.Unicode.GetString(plainBytes, 0, decrCount); } During decryption, I get the following error: "Length of the data to decrypt is invalid." on this line of code: int decrCount = cStream.Read(plainBytes, 0, plainBytes.Length); Please help. Thanx in advance.