本文介绍了RijndaelManaged - 填充无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用RijndaelManaged加密仅4字节的数据,产生16字节的结果(我使用128块大小)。当我尝试解密这16个字节时,我收到消息填充无效且无法删除。。



(CryptoStream的基本流是基于这16个字节的MemoryStream)。



有谁知道为什么?



谢谢

I am using RijndaelManaged to encrypt just 4 bytes of data which produces a 16 byte result (i am using a 128 block size). When I try to decrypt these 16 bytes I get the message "Padding is invalid and cannot be removed.".

(The base stream of the CryptoStream is a MemoryStream based on just these 16 bytes).

Anyone know why?

Thanks

推荐答案

public RijndaelManaged GetRijndaelManaged(String secretKey)
{
var keyBytes = new byte[16];
var secretKeyBytes = Encoding.UTF8.GetBytes(secretKey);
Array.Copy(secretKeyBytes, keyBytes, Math.Min(keyBytes.Length, secretKeyBytes.Length));
return new RijndaelManaged
{
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7,
KeySize = 128,
BlockSize = 128,
Key = keyBytes,
IV = keyBytes
};
}




这篇关于RijndaelManaged - 填充无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 20:24