在OpenSSL中使用零填充? [英] Use Zero Padding in OpenSSL?

查看:209
本文介绍了在OpenSSL中使用零填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Security.Cryptography.TripleDES允许我这样使用零填充:

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;
}

现在更改为使用OpenSSL

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

我想那是因为填充为零.但我不知道如何在openssl中使用零填充.

I guess that is because of the padding zero. But i don't know how to use padding zero in openssl.

感谢您的帮助.

推荐答案

Openssl不允许零填充",可能是因为结果不一致(如果明文的最后一个字节为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允许您使用PKCS填充或不使用填充(这要求输入的长度必须是块大小的倍数).

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).

如果要模拟此零填充",则需要自己添加一个适当的0,然后选择no padding选项.

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.

这篇关于在OpenSSL中使用零填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆