944,142 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1189
  • C# RSS
Sep 29th, 2009
0

Something wrong with the Decrypt function

Expand Post »
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.

C# Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
abhipro is offline Offline
21 posts
since Sep 2009
Sep 29th, 2009
0

Re: Something wrong with the Decrypt function

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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Declare 2 Dimension for List<String>
Next Thread in C# Forum Timeline: New to this, need help disecting a completed program to an editable form





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC