user3203425
user3203425

Reputation: 3059

Python to Java encryption/decryption, making sure cipher matches?

I'm looking at some common python example code which does encryption, setup like:

encryptor = AES.new(key, AES.MODE_CBC, iv)
chunksize = 64*1024

while True:
    chunk = infile.read(chunksize)
    if len(chunk) == 0:
        break
    elif len(chunk) % 16 != 0:
        chunk += ' ' * (16 - len(chunk) % 16)

    outfile.write(encryptor.encrypt(chunk))

Trying to set up java code to decrypt output from the above, I see examples that initialize the cipher padding differently:

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

or:

Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");

even though all the python snippets are implemented the same way.

I would expect to use "NoPadding" in this case. Can anyone explain what the correct selection in this case is?

Thank you

Upvotes: 0

Views: 812

Answers (1)

Duncan Jones
Duncan Jones

Reputation: 69339

The Python code is padding with spaces (' '), unless I misunderstand the code (not a Python guy). You will need to pad manually in your Java code and use "AES/CBC/NoPadding".

Upvotes: 1

Related Questions