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;
H H
  • 263,252
  • 30
  • 330
  • 514
ikel
  • 1,790
  • 6
  • 31
  • 61
  • What created the cipherdata to start with? Perhaps the error is there, or you've mismatched the padding etc? – Jon Skeet Nov 09 '11 at 07:33
  • u mean the key and byte[] to decrypt?? – ikel Nov 09 '11 at 07:39
  • this is just a sample code i borrowed from http://www.codeproject.com/KB/security/DotNetCrypto.aspx it used to work, but don't know why doesnt anymore – ikel Nov 09 '11 at 07:41
  • weird thing is, i can still build it, but when vs2010 tries to run, that is when the exception shows up – ikel Nov 09 '11 at 07:43
  • "it used to work" - go back to your backup of that time... – H H Nov 09 '11 at 08:10
  • It could be anything in the encoding+transport chain. How is your byte[] data stored? – H H Nov 09 '11 at 08:10

1 Answers1

0

Have you tried the following?

cs.Write(cipherData, 0, cipherData.Length);
//Add this line:
cs.FlushFinalBlock();
cs.Close();
Veldmuis
  • 4,678
  • 4
  • 25
  • 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