954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Something wrong with the Decrypt function

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.

public static string Decrypt(string   cipherText,
                                 string   passPhrase,
                                 string   saltValue,
                                 string   hashAlgorithm,
                                 int      passwordIterations,
                                 string   initVector,
                                 int      keySize)
    {

byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
        byte[] saltValueBytes  = Encoding.ASCII.GetBytes(saltValue);
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);

 PasswordDeriveBytes password = new PasswordDeriveBytes(
                                                        passPhrase, 
                                                        saltValueBytes, 
                                                        hashAlgorithm, 
                                                        passwordIterations);

byte[] keyBytes = password.GetBytes(keySize / 8);

 RijndaelManaged    symmetricKey = new RijndaelManaged();

symmetricKey.Mode = CipherMode.CBC;

ICryptoTransform decryptor = symmetricKey.CreateDecryptor(
keyBytes,  initVectorBytes);

MemoryStream  memoryStream = new MemoryStream(cipherTextBytes);
 CryptoStream  cryptoStream = new CryptoStream(memoryStream, 
decryptor, CryptoStreamMode.Read);

byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 
 0,  plainTextBytes.Length);

memoryStream.Close();
cryptoStream.Close();

string plainText = Encoding.UTF8.GetString(plainTextBytes, 
 0,  decryptedByteCount);

return plainText;

}


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

abhipro
Newbie Poster
21 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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.

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: