Hi all,

I am tying to test the code from the following url

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(

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);


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,


This article has been dead for over six months. Start a new discussion instead.