C# Rijndael CryptographicException "Length of the data to encrypt is invalid."

StackOverflow https://stackoverflow.com/questions/15731911

  •  30-03-2022
  •  | 
  •  

سؤال

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