Something wrong with the Decrypt function

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2009
Posts: 15
Reputation: abhipro is an unknown quantity at this point 
Solved Threads: 0
abhipro abhipro is offline Offline
Newbie Poster

Something wrong with the Decrypt function

 
0
  #1
Sep 29th, 2009
Hi all,

I am tying to test the code from the following url
http://www.obviex.com/Samples/Encryption.aspx

The following Decrypt code has some issues. It does not give any errors, but the plaintext is not returned either. The corresponding Encrypt code is working fine.

  1.  
  2. public static string Decrypt(string cipherText,
  3. string passPhrase,
  4. string saltValue,
  5. string hashAlgorithm,
  6. int passwordIterations,
  7. string initVector,
  8. int keySize)
  9. {
  10.  
  11. byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
  12. byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
  13. byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
  14.  
  15. PasswordDeriveBytes password = new PasswordDeriveBytes(
  16. passPhrase,
  17. saltValueBytes,
  18. hashAlgorithm,
  19. passwordIterations);
  20.  
  21. byte[] keyBytes = password.GetBytes(keySize / 8);
  22.  
  23. RijndaelManaged symmetricKey = new RijndaelManaged();
  24.  
  25. symmetricKey.Mode = CipherMode.CBC;
  26.  
  27. ICryptoTransform decryptor = symmetricKey.CreateDecryptor(
  28. keyBytes, initVectorBytes);
  29.  
  30. MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
  31. CryptoStream cryptoStream = new CryptoStream(memoryStream,
  32. decryptor, CryptoStreamMode.Read);
  33.  
  34. byte[] plainTextBytes = new byte[cipherTextBytes.Length];
  35. int decryptedByteCount = cryptoStream.Read(plainTextBytes,
  36. 0, plainTextBytes.Length);
  37.  
  38. memoryStream.Close();
  39. cryptoStream.Close();
  40.  
  41. string plainText = Encoding.UTF8.GetString(plainTextBytes,
  42. 0, decryptedByteCount);
  43.  
  44. return plainText;
  45.  
  46. }

When I provide wrong key during the decryption, i.e. different from what was used in Encrypt function, an error is prompted

Padding is invalid and cannot be removed

This means, that the code is functional but something is not allowing the result to return.

Thanks in advance,

Abhi
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,212
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 572
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Something wrong with the Decrypt function

 
0
  #2
Sep 29th, 2009
Are you fond of that particular encryption method or are you just want any type of encryption? I have a code sample online at http://www.daniweb.com/code/snippet217409.html with examples of encrypt and decrypt routines.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

Tags
encryption

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC