I am trying to encrypt and decrypt a file with the rijndael algorythm, but i have been getting the error "Length of the data to encrypt is invalid.". I am able to encrypt the file, but i can't decrypt it. This is my decryption function;

    public static byte[] Decrypt(byte[] toEncryptArray)
    {
        byte[] keyArray = UTF8Encoding.UTF8.GetBytes("-key-");
        RijndaelManaged rDel = new RijndaelManaged();
        rDel.Key = keyArray;
        rDel.Padding = PaddingMode.PKCS7;
        ICryptoTransform cTransform = rDel.CreateDecryptor();
        return cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    }

I honestly have no idea what i am doing wrong, as i can encrypt it perfectly fine. The file i am trying to decrypt is 11 kb.

有帮助吗?

解决方案

You should be using the CryptoStream object, which will automatically call the correct ICryptoTransform.TransformFinalBlock and ICryptoTransform.TransformBlock methods.

You haven't posted the encryption code but check that the Padding mode the same (i.e. PaddingMode.PKCS7) and the initiation vector is set to the same string.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top