Hi I would really appreciate the help on this one since i'm really lost and I don't get why it's not working.
I have a 16 byte key and 16 byte block but the key type is 'str' and the block type is 'bytes' and I want to xor between them but the value is incorrect(I think) this code is based on this post
def xor(data, key):
if type(key) != str:
key = str(key)
l = len(key)
buff=""
for i in range(0,len(data)):
buff+=chr(data[i]^ord(key[i]))
return str.encode(buff)
and in context:(ref to CBC)
def CBC_decrypt(blocklist,IV,decrypter):
'''
:param blocklist: a 16 byte item list(or other iteratable)
:param IV: 16 byte sized iv
:param decrypter: decrypter: a generic decrypter-must provide an decrypt(plaintext) method
where plaintext is 16 byte size
:return: a list of decrypted blocks of 16 bytes
'''
decrypted_msg=[]
prev = IV
for x in blocklist:
r = xor(decrypter.decrypt(x),prev)
decrypted_msg.append(r)
prev = x
return decrypted_msg
a = FileTo16ByteBlocks(path) #just read 16 bytes at a time and return a list
cipher2= AES.new(loadKey(),AES.MODE_ECB) #init AES in ECB cause we had to implement CBC
d = CBC_decrypt(a,iv[0],cipher2)
and here i'm writing it all to the file
# create new file iterate through the deciphered data and write it to that file
with open(str(fileSeq)+'.mp4','wb') as f:
for i in d:
f.write(i)
f.write(b'')
I've tried other things regarding the xor (since the ECB decryption is made by the library-pycrypto) like using it's number.bytes to long and xoring that and I've tried to cast it to ints and see how it goes, all goes really bad and the xor thing is just a hunch I can't really lay my finger on why this is not working properly! thanks for all the helpers
I've managed to solve it! the problem was indeed with the xor function as i thought, apparently the length of the result of the xor wasn't 16 but because of conversions it was variant(why is that though?)
I'm adding the code here incase anyone will need it in the future
def xor(data, key1):
b=key1
if type(key1) is str:
b = bytes(key1,encoding='utf8')
iData = int.from_bytes(data, sys.byteorder)
iKey = int.from_bytes(b, sys.byteorder)
xored = iData ^ iKey
xored = xored.to_bytes(len(data), sys.byteorder)
return xored