DES 解密错误“坏数据”在 C# 中关闭 cryptostream 时
我尝试解密加密的字节数组(使用 K1 加密并使用 K2 解密)。当 Visual Studio 尝试关闭我的加密流时,它会抛出异常“BAD DATA”。
这是我的 DES 解密代码片段,
public Byte[] Decrypt(Byte[] cipherData, Byte[] key, Byte[] iv)
{
MemoryStream ms = new MemoryStream();
DES mDES = DES.Create();
mDES.Key = key;
mDES.IV = iv;
mDES.Padding = PaddingMode.PKCS7;
CryptoStream cs = new CryptoStream(ms, mDES.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(cipherData, 0, cipherData.Length);
cs.Close();
Byte[] decryptedData = ms.ToArray();
return decryptedData;
}
初始向量与加密相同。我不知道为什么会出现这个错误。
添加: 根据 Greg B 的推荐,我在这里发布了我的加密代码片段。加密的输出是解密的输入(两个不同的密钥)
public Byte[] Decrypt(Byte[] cipherData, Byte[] key, Byte[] iv)
{
MemoryStream ms = new MemoryStream();
DES mDES = DES.Create();
mDES.Key = key;
mDES.IV = iv;
mDES.Padding = PaddingMode.PKCS7;
CryptoStream cs = new CryptoStream(ms, mDES.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(cipherData, 0, cipherData.Length);
cs.Close();
Byte[] decryptedData = ms.ToArray();
return decryptedData;
}
I try to decrypt an encrypted byte array (encrypt with K1 and decrypt with K2). Visual Studio throws an exception "BAD DATA" when it tries to close my crypto stream
here's my code snippet of DES decryption
public Byte[] Decrypt(Byte[] cipherData, Byte[] key, Byte[] iv)
{
MemoryStream ms = new MemoryStream();
DES mDES = DES.Create();
mDES.Key = key;
mDES.IV = iv;
mDES.Padding = PaddingMode.PKCS7;
CryptoStream cs = new CryptoStream(ms, mDES.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(cipherData, 0, cipherData.Length);
cs.Close();
Byte[] decryptedData = ms.ToArray();
return decryptedData;
}
the initial vector is the same as encryption. I don't know why this error occurred.
Added:
As recommended by Greg B, I post here my code snippet of encryption. The output of encryption is the input of decryption (two different keys)
public Byte[] Decrypt(Byte[] cipherData, Byte[] key, Byte[] iv)
{
MemoryStream ms = new MemoryStream();
DES mDES = DES.Create();
mDES.Key = key;
mDES.IV = iv;
mDES.Padding = PaddingMode.PKCS7;
CryptoStream cs = new CryptoStream(ms, mDES.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(cipherData, 0, cipherData.Length);
cs.Close();
Byte[] decryptedData = ms.ToArray();
return decryptedData;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您遇到的问题来自于所选的填充模式。
由于在解密密文时使用填充,因此当您检索解密的字节时,加密流会尝试删除填充。由于您不使用数据加密所用的密钥,因此您将得到“垃圾”...加密流无法检测到需要删除的填充,并且操作失败并出现观察到的异常。
如果您想使用 DES 类重建 3DES 等内容,请使用 PaddingMode NONE 进行解密和以下加密步骤(因此只有第一次加密或最后一次解密使用填充)
the problem you encountered comes from the selected padding mode.
since padding is used when decrypting your cipher text the cryptostream tries to remove the padding when your retrieve the decrypted bytes. since you don't use the key the data was encrypted with, you will get "garbage" ... the cryptostream fails to detect the padding that needs to be removed, and the operation fails with the observed exception.
if you want to rebuld someething like 3DES with the DES class use PaddingMode NONE for your decryption and the following encryption step (so only the first encryption or the last decryption uses padding)