I'm trying to encrypt some data using triple DES ECB, but I get a discrepancy between OpenSSL and the pyDES library I'm using.
When trying to get a KCV value, I'm using a file consisting of 8 bytes each with value 0.
I'm using the following OpenSSL command (des-ede3 chosen based on this):
openssl enc -des-ede3 -pass pass:abcdefgh12345678abcdefgh -in empty8.bin -nosalt | xxd
which results in:
00000000: 17f7 b498 bd79 4d55 5e40 aa9c 2907 44bb .....yMU^@..).D.
I'm using the following Python script, based on this question:
import binascii
import hashlib
import base64
from pyDes import *
key = 'abcdefgh12345678abcdefgh'
fileInput = open("empty8.bin", "rb")
data = fileInput.read()
print('key: ' + str(key))
k = triple_des(key, ECB)
d = k.encrypt(data)
print('KCV: ' + str(binascii.hexlify(d)[0:8]))
which results in:
key: abcdefgh12345678abcdefgh
KCV: b'37c92746'
I've also tried using http://online-domain-tools.com/ for a reference, which gives the same result as the Python code.
Can anyone offer any insights into why OpenSSL produces a different result?
-K
rather than-pass
; make the key hex in Pyhton; and use the same size (24-or-is-it-16 bytes, twice as many hex chars). 2) the result you get with OpenSSL is 16-byte, twice the blocksize of DES, and it suggests a header or/and padding when none is wanted; perhaps adding-nopad
to the openssl options would fix that? $\endgroup$openssl enc -des-ede3-cbc -K 616263646566676831323334353637386162636465666768 -iv 0000000000000000 -in kcvin.dat -out kcvout.dat
, except that it is too long by 8 bytes of course. You can usedd
to create the 8 byte zero input and of course copy out the 8 bytes required. PS usehexdump
instead, that crappy hex output has little endian encoding. $\endgroup$xxd
output is default grouped by 2 (-g1
to fix) but never little-endian; it isod -x
(orod -tx{otherthan1}
) that is platform-endian which is little-endian on x86[-64] and some others. As fgrieu said,enc -nopad
avoids the extra block=8bytes. ECB (and not CBC) is correct for a KCV, though not for anything else except possibly keywrap. $\endgroup$