"rijndaelmanaged “padding is invalid and cannot be removed” that only occurs when decrypting in production" Code Answer

2

you will sometimes get a message about invalid padding when encryption and decryption for whatever reason have not used the same key or initialisation vector. padding is a number of bytes added to the end of your plaintext to make it up to a full number of blocks for the cipher to work on. in pkcs7 padding each byte is equal to the number of bytes added, so it can always be removed after decryption. your decryption has led to a string where the last n bytes are not equal to the value n of the last byte (hope that sentence makes sense). so i would double check all your keys.

alternatively, in your case, i would suggest making sure that you create and dispose an instance of rijndaelmanagedtransform for each encryption and decryption operation, initialising it with the key and vector. this problem could very well be caused by reusing this transform object, which means that after the first use, it is no longer in the right initial state.

By Tareq El-Masri on May 10 2022

Answers related to “rijndaelmanaged “padding is invalid and cannot be removed” that only occurs when decrypting in production”

Only authorized users can answer the Search term. Please sign in first, or register a free account.