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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.