I'm working on a server application in Python and an app to connect to it in Java. I need to encrypt the string in Java and then decrypt it in Python.
Everything works well with the Java code, but there is a problem with the Python code.
from Crypto.Cipher import AES
obj = AES.new('0123456789abcdef', AES.MODE_CBC,IV="fedcba9876543210")
BLOCK_SIZE = 16
PADDING = " "
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
plain = "password"
plain = pad (plain)
ciph = obj.encrypt(plain)
obj = AES.new('0123456789abcdef', AES.MODE_CBC,IV="fedcba9876543210")
decrypted = obj.decrypt(ciph)
result = ""
for char in ciph:
result += str((hex(ord(char))))[2:]
print result
print decrypted
The results of this are:
2af41fc02b33765b56433f59edb67dd3
password
Which is what I would expect and matches the Java output, however when I plug in the first of the 2 lines to decrypt it in the Python code, the output is totally off and displays random characters. I think that is due to the for loop at the end which makes it the same as the Java output. Is there a way to undo that for loop? Also using code at least similar to this is there a decent way to decrypt the first of the two output strings (in Python)?
I appreciate all responses, thank you ahead of time!