Search code examples
python-3.xcryptographypycryptodescbc-mode

Python3 PyCrypto produce a Valueerror when decrypt long message


Hy,

i must decrypt a DES encrypted message with DES and XOR.
When i decrypt a 24 bytes long message, it runs correctly.
But with 40 bytes and more, produce an error like this:

result4 = XOR.new(message_bytes[i-(blocksize*2):-blocksize]).encrypt(result3)
File "/usr/lib/python3/dist-packages/Crypto/Cipher/XOR.py", line 80, in new
    return XORCipher(key, *args, **kwargs)
File "/usr/lib/python3/dist-packages/Crypto/Cipher/XOR.py", line 44, in __init__
    self._cipher = _XOR.new(key, *args, **kwargs)
    ValueError: XOR key must be no longer than 32 bytes  

I encrypt the message with this code:

def like_3DES_encrypt(message_bytes, key_16bytes_hex):
    blocksize = 8
    data_b_pad = appendBitPadding(message_bytes, 8)
    key_b = bytes.fromhex(BitArray(hex=key_16bytes_hex).hex)
    iv = bytes.fromhex(BitArray(hex='0000000000000000').hex)
    # split the key on half size
    key_1_b = key_b[:8]
    key_2_b = key_b[8:16]
    result = b''

    for i in range(0, len(data_b_pad), blocksize):
        block = data_b_pad[i:i+blocksize]
        if i > 0:
            result0 = XOR.new(result3).encrypt(block)
        else:
            result0 = block
        result1 = DES.new(key_1_b, DES.MODE_CBC, iv).encrypt(result0)
        result2 = DES.new(key_2_b, DES.MODE_CBC, iv).decrypt(result1)
        result3 = DES.new(key_1_b, DES.MODE_CBC, iv).encrypt(result2)
        result += result3

    return result  

The encryption works without Error.

But when i try to decrypt the encrypted message with this code:

def like_3DES_decrypt(message_bytes, key_16bytes_hex):
    blocksize = 8
    key_b = bytes.fromhex(BitArray(hex=key_16bytes_hex).hex)
    iv = bytes.fromhex(BitArray(hex='0000000000000000').hex)
    # split the key on half size
    key_1_b = key_b[:8]
    key_2_b = key_b[8:16]
    result = b''

    for i in range(len(message_bytes), 0, - blocksize):
        block = message_bytes[i-blocksize:i]
        result1 = DES.new(key_1_b, DES.MODE_CBC, iv).decrypt(block)
        result2 = DES.new(key_2_b, DES.MODE_CBC, iv).encrypt(result1)
        result3 = DES.new(key_1_b, DES.MODE_CBC, iv).decrypt(result2)

        if i > blocksize:
            result4 = XOR.new(message_bytes[i-(blocksize*2):-blocksize]).encrypt(result3)
        else:
            result4 = result3
        result = result4 + result

    result = removeBitPadding(result)
    return result

What is wrong when i decrypt the long message?

Thank you


Solution

  • The error message ValueError: XOR key must be no longer than 32 bytes tells you that you cannot initialize an XOR object with a key larger than 32 bytes. By inspecting the source code you can see this is enforced in line 61. To perform an xor with larger keys you'll need a different implementation.

    Your line

    result4 = XOR.new(message_bytes[i-(blocksize*2):-blocksize]).encrypt(result3)

    seems a bit odd though, maybe you have swapped the message with the key:

    result4 = XOR.new(result3).encrypt(message_bytes[i-(blocksize*2):-blocksize])