使用 DES3 加密 3000 字节及更多
我尝试使用 DES3 加密数据:
private byte[] EncryptKey(byte[] wrappedKey)
{
MemoryStream ms = new MemoryStream(wrappedKey);
TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider();
cryptoProvider.Key = transportKeyBytes; //key
cryptoProvider.Padding = PaddingMode.None;
cryptoProvider.Mode = CipherMode.ECB;
CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateEncryptor(), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
byte[] key = new Byte[wrappedKey.Length];
cs.Read(key, 0, wrappedKey.Length);
cs.Close();
ms.Close();
return key;
}
但我收到错误消息:
Length of the data to encrypt is invalid.
可以加密的数据的最大大小是多少?
Im trying to encrypt data with DES3:
private byte[] EncryptKey(byte[] wrappedKey)
{
MemoryStream ms = new MemoryStream(wrappedKey);
TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider();
cryptoProvider.Key = transportKeyBytes; //key
cryptoProvider.Padding = PaddingMode.None;
cryptoProvider.Mode = CipherMode.ECB;
CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateEncryptor(), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
byte[] key = new Byte[wrappedKey.Length];
cs.Read(key, 0, wrappedKey.Length);
cs.Close();
ms.Close();
return key;
}
But im getting error saying:
Length of the data to encrypt is invalid.
Whats the maximum size of the data can be encrypted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有使用任何填充。这可能是你的错误的原因。如果不添加任何填充,您只能加密确切数量的 8 字节块。对于 DES,将填充更改为 PKCS#5。
ECB 模式并不安全。请参阅维基百科了解(字面)说明。请改用 CBC 或 CTR 模式。
3DES 已过时。除非您必须使用它来与旧系统兼容,否则请使用 AES。
You are not using any padding. That is probably the cause of your error. By not adding any padding you are only able to encrypt an exact number of 8 byte blocks. Change the padding to PKCS#5 for DES.
ECB mode is not secure. See Wikipedia for a (literal) illustration. Use CBC or CTR mode instead.
3DES is obsolete. Unless you have to use it for compatibility with an old system use AES instead.