Now works on py3 also
This commit is contained in:
parent
33b5708da3
commit
9df14fa6a6
@ -16,7 +16,7 @@ from ige import ige
|
|||||||
# message must be a multiple of 16 in size
|
# message must be a multiple of 16 in size
|
||||||
msg_to_encrypt = "This is a secret message"
|
msg_to_encrypt = "This is a secret message"
|
||||||
padding_needed = 16 - len(msg_to_encrypt) % 16
|
padding_needed = 16 - len(msg_to_encrypt) % 16
|
||||||
msg_to_encrypt_padded = msg_to_encrypt + b'0' * padding_needed
|
msg_to_encrypt_padded = msg_to_encrypt + str(0) * padding_needed
|
||||||
print("Encrypting: '" + str(msg_to_encrypt) + "'")
|
print("Encrypting: '" + str(msg_to_encrypt) + "'")
|
||||||
print("With padding: '" + str(msg_to_encrypt_padded) + "'")
|
print("With padding: '" + str(msg_to_encrypt_padded) + "'")
|
||||||
# 32 bytes long key
|
# 32 bytes long key
|
||||||
|
19
ige.py
19
ige.py
@ -42,12 +42,19 @@ def hex_string_to_long(val):
|
|||||||
Convert it to int, which is actually long"""
|
Convert it to int, which is actually long"""
|
||||||
return int(val, 16)
|
return int(val, 16)
|
||||||
|
|
||||||
def xor_strings(a, b): # xor two strings of different lengths
|
def xor_stuff(a, b):
|
||||||
if version_info <= (3, 4, 0):
|
"""XOR applied to every element of a with every element of b.
|
||||||
|
Depending on python version and depeding on input some arrangements need to be done."""
|
||||||
|
if version_info < (3, 4, 0):
|
||||||
if len(a) > len(b):
|
if len(a) > len(b):
|
||||||
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])
|
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])
|
||||||
else:
|
else:
|
||||||
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])])
|
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])])
|
||||||
|
else:
|
||||||
|
if type(a) == str and type(b) == bytes:# cipher.encrypt returns string
|
||||||
|
return bytes(ord(x) ^ y for x, y in zip(a, b))
|
||||||
|
elif type(a) == bytes and type(b) == str:
|
||||||
|
return bytes(x ^ ord(y) for x, y in zip(a, b))
|
||||||
else:
|
else:
|
||||||
return bytes(x ^ y for x, y in zip(a, b))
|
return bytes(x ^ y for x, y in zip(a, b))
|
||||||
|
|
||||||
@ -88,11 +95,15 @@ def ige(message, key, iv, operation="decrypt"):
|
|||||||
for i in range(0, len(message), blocksize):
|
for i in range(0, len(message), blocksize):
|
||||||
indata = message[i:i+blocksize]
|
indata = message[i:i+blocksize]
|
||||||
if operation == "decrypt":
|
if operation == "decrypt":
|
||||||
outdata = xor_strings(cipher.decrypt(xor_strings(indata, ivp2)), ivp)
|
xored = xor_stuff(indata, ivp2)
|
||||||
|
decrypt_xored = cipher.decrypt(xored)
|
||||||
|
outdata = xor_stuff(decrypt_xored, ivp)
|
||||||
ivp = indata
|
ivp = indata
|
||||||
ivp2 = outdata
|
ivp2 = outdata
|
||||||
elif operation == "encrypt":
|
elif operation == "encrypt":
|
||||||
outdata = xor_strings(cipher.encrypt(xor_strings(indata, ivp)), ivp2)
|
xored = xor_stuff(indata, ivp)
|
||||||
|
encrypt_xored = cipher.encrypt(xored)
|
||||||
|
outdata = xor_stuff(encrypt_xored, ivp2)
|
||||||
ivp = outdata
|
ivp = outdata
|
||||||
ivp2 = indata
|
ivp2 = indata
|
||||||
else:
|
else:
|
||||||
|
Loading…
Reference in New Issue
Block a user