0

From a comment:

The problem was gone once i manually set padding to NONE


whats wrong with this code?? VS2010 does compile it, but it gives error when run from VS2010, saying cs.close() padding is not valid, can anyone help? thanks

public static byte[] Decrypt(byte[] cipherData,byte[] Key, byte[] IV)
{
 MemoryStream ms = new MemoryStream();
  Rijndael alg = Rijndael.Create();

        alg.Key = Key;
        alg.IV = IV;
        alg.Padding = PaddingMode.PKCS7, ;

        CryptoStream cs = new CryptoStream(ms,
            alg.CreateDecryptor(), CryptoStreamMode.Write);



        cs.Write(cipherData, 0, cipherData.Length);


        cs.Close();


        byte[] decryptedData = ms.ToArray();

        return decryptedData;
Henk Holterman
  • 250,905
  • 30
  • 306
  • 490
ikel
  • 1,620
  • 5
  • 29
  • 54

1 Answers1

0

Have you tried the following?

cs.Write(cipherData, 0, cipherData.Length);
//Add this line:
cs.FlushFinalBlock();
cs.Close();
Veldmuis
  • 4,488
  • 4
  • 24
  • 25
  • ok, then like Jon Skeet said - you must be sure that the cipherData you are trying to decrypt was encrypted using the same Padding that you are using for the decryption (PaddingMode.PKCS7). – Veldmuis Nov 09 '11 at 07:49
  • Where does cipherData come from? If you don't set the encryption padding explicitly it might not always default to the same value - maybe you should try to set it if you can? You say it works on XP - is it breaking on Win7? – Veldmuis Nov 09 '11 at 07:58
  • i think you made point, the decryption algorithm might add paddings itself. The problem was gone once i manually set padding to NONE – ikel Nov 09 '11 at 15:06