Using AES encryption in .NET - CryptographicException saying the padding is invalid and cannot be removed

梦想的初衷 提交于 2019-11-29 07:22:44

When using a block cipher like AES in a mode that requires padding, like CBC, you must be aware that the output will always be a multiple of the block size. To accomplish this, padding modes like PKCS7 will add some bytes to the cipher at the end of the encryption process. But you have to let the encryptor know when the end occurs. To do so, all you have to do is insert the statement

cryptoStream.FlushFinalBlock();  

after

cryptoStream.Write(rawData, 0, rawData.Length);

PS:

Perhaps it is just for debugging, but your salt generation method generates the exact same salt every time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!