#1 2018-04-11 13:54:53

Denver
Member
Registered: 2018-04-11
Posts: 5

SynCrypto CFB/python

#
# Im trying to interop Syncrypto with python, i have checked the iv/key/mode and they all match in Embarcadero® Delphi 10.1 Berlin Version 24.0.22858.6822  / python 3.6
# for now im ignoring the pkcs7 padding, python result is the same as using http://aes.online-domain-tools.com/
# i have tried with syncrypto using its native asm code and its pascal code, both produce same results, which is different from python code below
#
# on python code, only the first byte is decrypted correctly, if anyone else has any idea of what could be happening before i dig deeper into AES code, please let me know. thank you!
#
#
# delphi code
# inp := TAESCFB.SimpleEncrypt('test passed', 'testing', true, true, true);
# FileFromString(inp,'out.txt');
# inp := StringFromFile('out.txt');
# out := TAESCFB.SimpleEncrypt(inp, 'testing',false, true);


from Crypto.Cipher import AES
from Crypto.Hash import SHA256

def makekey(key):
    kl = chr(len(key))
    for i in range(0, 256-len(key)):
        key += kl
    return SHA256.new(key.encode("utf-8")).digest()

if __name__ == "__main__":

    key = makekey("testing")
    encrypted = b'\xB3\x73\xBC\xD6\xBF\xF0\x41\x73\x17\x02\x15\x51\xB1\x09\x18\x43\x50\x1D\x75\xFA\x03\x2D\x77\x9E\x5D\x15\x42\x31\x53\x2B\x0B\x84'

    iv = encrypted[0:16]
    data = encrypted[16:]

    cipher = AES.new(key, AES.MODE_CFB, IV=iv)
    decrypted = cipher.decrypt(data)

    for i in decrypted:
        print("{:02X} ".format(i), end='')
    # should have decrypted to 74 65 73 74 20 70 61 73 73 65 64

Offline

#2 2018-04-11 14:06:27

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,669
Website

Re: SynCrypto CFB/python

The AES code is correct in SynCrypto, you can be sure of that. smile

I assume that your makekey() does not match the Delphi part.
Use a raw array of bytes too for the key itself.

Offline

#3 2018-04-11 18:32:53

Denver
Member
Registered: 2018-04-11
Posts: 5

Re: SynCrypto CFB/python

Hello,

thanks for answering, i have double checked, the return from makekey function is already an array of bytes.

I have tried declaring it directly as:

key = b'\xBE\x31\xFB\x56\x2C\xFA\xED\x2B\x2E\xBF\x40\x57\x47\xE0\x77\x87\x5E\xE3\x00\xCE\x5B\x6C\xEF\x45\xEE\x5D\x3C\x39\xED\xB0\x41\x62'

but i got the same result, this is the key from delphi perspective. i have compared both and they match

keydelphi.png


this is the iv:

iv = b'\xB3\x73\xBC\xD6\xBF\xF0\x41\x73\x17\x02\x15\x51\xB1\x09\x18\x43'

which from delphi perspective is:

ivdelphi.png

this is the python snippted simplified:
    key = b'\xBE\x31\xFB\x56\x2C\xFA\xED\x2B\x2E\xBF\x40\x57\x47\xE0\x77\x87\x5E\xE3\x00\xCE\x5B\x6C\xEF\x45\xEE\x5D\x3C\x39\xED\xB0\x41\x62'
    iv = b'\xB3\x73\xBC\xD6\xBF\xF0\x41\x73\x17\x02\x15\x51\xB1\x09\x18\x43'
    encrypted = b'\x50\x1D\x75\xFA\x03\x2D\x77\x9E\x5D\x15\x42\x31\x53\x2B\x0B\x84'

    data = encrypted

    cipher = AES.new(key, AES.MODE_CFB, IV=iv)
    decrypted = cipher.decrypt(data)

    for i in decrypted:
        print("{:02X} ".format(i), end='')
    # should have decrypted to 74 65 73 74 20 70 61 73 73 65 64

Offline

#4 2018-04-12 08:30:42

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,669
Website

Re: SynCrypto CFB/python

Did you try with another mode?

Offline

#5 2018-04-12 11:32:25

Denver
Member
Registered: 2018-04-11
Posts: 5

Re: SynCrypto CFB/python

Yes,

CBC mode works fine both in python and delphi code.

Offline

#6 2018-04-12 13:37:28

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,669
Website

Re: SynCrypto CFB/python

and OFB?

Offline

#7 2018-04-12 13:44:49

Denver
Member
Registered: 2018-04-11
Posts: 5

Re: SynCrypto CFB/python

OFB works too.

Offline

#8 2018-04-12 20:05:03

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,669
Website

Re: SynCrypto CFB/python

It is weird, because CFB has the proper chaining algorithm...
Do you see anything strange in our implementation?

Offline

#9 2018-04-16 12:18:11

Denver
Member
Registered: 2018-04-11
Posts: 5

Re: SynCrypto CFB/python

Hello,

I was able to decrypt (delphi) TAESCFB using MODE_OFB in python.

Offline

Board footer

Powered by FluxBB