开发者

Use Zero Padding in OpenSSL?

开发者 https://www.devze.com 2023-03-07 08:28 出处:网络
System.Security.Cryptography.TripleDES allow me use zero pad开发者_JS百科ding like this : static TripleDES CreateTripleDES(byte[] key, byte[] iv)

System.Security.Cryptography.TripleDES allow me use zero pad开发者_JS百科ding 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号