6

Please suggest me where i need to update/refactor the code to get rid of exception. I am getting exception while I try to decrypt the encrypted string using following code.

Following line is throwing exception

using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { // Read the decrypted bytes from the decrypting stream // and place them in a string. plaintext = srDecrypt.ReadToEnd(); } public string EncryptAuthenticationTokenAes(string plainText) { byte[] encrypted; // Create an AesManaged object // with the specified key and IV. using (AesManaged aesAlg = new AesManaged()) { // Create a decrytor to perform the stream transform. ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); aesAlg.Padding = PaddingMode.None; // Create the streams used for encryption. using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { //Write all data to the stream. swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray(); } } } // Return the encrypted bytes from the memory stream. return Convert.ToBase64String(encrypted); } public string DecryptPasswordAes(string encryptedString) { //Convert cipher text back to byte array byte[] cipherText = Convert.FromBase64String(encryptedString); // Declare the string used to hold // the decrypted text. string plaintext = null; // Create an AesManaged object // with the specified key and IV. using (AesManaged aesAlg = new AesManaged()) { // Create a decrytor to perform the stream transform. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); aesAlg.Padding = PaddingMode.None; // Create the streams used for decryption. using (MemoryStream msDecrypt = new MemoryStream(cipherText)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { // Read the decrypted bytes from the decrypting stream // and place them in a string. plaintext = srDecrypt.ReadToEnd(); } } } } return plaintext; } 
1
  • I found actually the problem in calling of encryption and decryption methods, as the both are using different Key and IV. when i passed the same Key and IV the problem get resolved. Commented May 9, 2012 at 7:06

2 Answers 2

19

Pretty standard bug when using CryptoStream, you forgot to force it to encrypt the last bytes of the stream. It keeps bytes in an internal buffer until enough of them arrive to emit a block. You must force the last few bytes out. Fix:

 using (var msEncrypt = new MemoryStream()) using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) using (var swEncrypt = new StreamWriter(csEncrypt)) { swEncrypt.Write(plainText); csEncrypt.FlushFinalBlock(); encrypted = msEncrypt.ToArray(); } 

You got the exception when decrypting it because encrypted is missing the final padding. The real problem is caused by the using statement, you wouldn't have this problem if you waited obtaining the encrypted bytes until after the CryptoStream is closed. But that doesn't work well because the using statement on the StreamWriter also closes the CryptoStream and the MemoryStream. Explicitly using FlushFinalBlock() is the best workaround.

Sign up to request clarification or add additional context in comments.

6 Comments

For some reason that approach did not work for me in C# 4.0/4.5 - had to put curly braces after each using( ). Than no .FlushFinalBlock( ) call is needed. See stackoverflow.com/questions/29809687/#29835039.
@Astrogator: That makes no sense and was not the source/fix. Curlies are optional and do not (in this case) change the semantics.
@EdS. Not the cause for this one, but check my linked question. I also decided that curlies {} were unnecessary - and got bitten by that. They may not change semantics, but they certainly did affect execution order because restoring them fixed my code. That's all i wanted to warn about.
@Astrogator: Oh, well sure; you moved a line of close to a completely different scope.
@EdS. The order of execution of finalisers (and => object closures), affected by presence or absence of curlies {}, was important in my question. I asked it, answered it myself, shared my experience, and warned others who'd stumble on this Q. 4 years later you just came to this one, dissed my comment, and then downvoted my answer. Not cool, dude!
|
0
 swEncrypt.Write(plainText); swEncrypt.Flush(); csEncrypt.FlushFinalBlock(); encrypted = msEncrypt.ToArray(); 

Calling swEncrypt.Flush() before csEncrypt.FlushFinalBlock() solved my problem.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.