在 OpenSSL 中使用零填充?
System.Security.Cryptography.TripleDES 允许我像这样使用零填充:
static TripleDES CreateTripleDES(byte[] key, byte[] iv)
{
TripleDES des = new TripleDESCryptoServiceProvider();
des.Key = key;
des.IV = iv;
des.Mode = CipherMode.CBC;
des.Padding = PaddingMode.Zeros;
return des;
}
现在更改为使用 OpenSSL
CipherContext cc = new CipherContext(Cipher.DES_EDE3_CBC);
byte[] des3 = cc.Encrypt(msg2, tripleKey, tripleIv, 0);
//public byte[] Encrypt(byte[] input, byte[] key, byte[] iv, int padding);
问题是,当我通过两种方法加密字符串时,我收到两个差异结果:
first method (OK): 90dd67c475dc3a8ce3d0c8927ce43758715888c16688c9828ac92aa86019126297b7ccb80a4729224acd07285b85a847e48fb01b3da4639c
second method (NOT OK): 90dd67c475dc3a8ce3d0c8927ce43758715888c16688c9828ac92aa86019126297b7ccb80a4729224acd07285b85a847b77485eb0a1f214e
我猜这是因为填充零。但我不知道如何在 openssl 中使用填充零。
任何帮助表示赞赏。
System.Security.Cryptography.TripleDES allow me use zero padding like this :
static TripleDES CreateTripleDES(byte[] key, byte[] iv)
{
TripleDES des = new TripleDESCryptoServiceProvider();
des.Key = key;
des.IV = iv;
des.Mode = CipherMode.CBC;
des.Padding = PaddingMode.Zeros;
return des;
}
Now change to use OpenSSL
CipherContext cc = new CipherContext(Cipher.DES_EDE3_CBC);
byte[] des3 = cc.Encrypt(msg2, tripleKey, tripleIv, 0);
//public byte[] Encrypt(byte[] input, byte[] key, byte[] iv, int padding);
The problem is that when i encrypt a string by two methods, i receive two diffences result :
first method (OK): 90dd67c475dc3a8ce3d0c8927ce43758715888c16688c9828ac92aa86019126297b7ccb80a4729224acd07285b85a847e48fb01b3da4639c
second method (NOT OK): 90dd67c475dc3a8ce3d0c8927ce43758715888c16688c9828ac92aa86019126297b7ccb80a4729224acd07285b85a847b77485eb0a1f214e
I guess that is because of the padding zero. But i don't know how to use padding zero in openssl.
Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Openssl 不允许“零填充”,可能是因为结果不一致(如果明文的最后一个字节是 0 怎么办...)
Openssl 将允许您使用 PKCS 填充或无填充(这需要输入是块大小长度的倍数)。
如果您想模拟这种“零填充”,您需要自己添加适当数量的 0,然后选择无填充选项。
Openssl doesn't allow for "zero padding", probably because the result is not consistent (what if the last byte of plaintext is a 0...)
Openssl will let you use either PKCS padding or no padding (which requires the input to be a multiple of the block size in length).
If you want to emulate this "zero padding", you would need to append an appropriate amount of 0's yourself, and then choose the no padding option.