pythonencryptiondes3destripledes

Python TripleDES (3DES) Hexa input Encryption


Python 3DES doesn't output the correct answer.

    import pyDes
    import binascii

    text = '111122223333AAAA44446666BBBBEEEE'
    binaryText = binascii.unhexlify(text)

    key = '123412341234ABCD'

    k = pyDes.triple_des(key, pyDes.ECB)

    encrypted = k.encrypt(binaryText)

    print("Encrypted Value = %r" % binascii.hexlify(encrypted))

    # decrypted = k.decrypt(encrypted)
    # print("Decrypted Value = %r" %binascii.hexlify(decrypted))

Encrypted value should be : 569F2551E1749FEE9221B20C4F76CD4B

3DES Online Tool : http://tripledes.online-domain-tools.com/


Solution

  • There are two reasons in the code why the results differ:

    So that the Python-code provides the result of the web-site

    key = '123412341234ABCD'
    

    has to be replaced by

    #key = '123412341234ABCD'                                   # 8 bytes key works for DES, but fails for TDES
    #key = '123412341234ABCD123412341234ABCD'                   # 16 bytes key or               
    key = '123412341234ABCD123412341234ABCD123412341234ABCD'    # 24 bytes key works for TDES
    key = binascii.unhexlify(str.encode(key, 'Utf-8'))          # convert hexadecimal string to binary data
    

    Be aware that the code is insecure in several ways: First, 1TDEA has practically the strength of DES and is therefore insecure. Even if TDES is used with the 3TDEA-variant (with a security comparable to AES), the more performant AES should be used. Second, the ECB-mode is insecure.