-2

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

Community
  • 1
  • 1
LiorA
  • 441
  • 3
  • 13
  • Can you show in detail WHAT is in your opinion incorrect, what is not working properly and how should the proper outcome look like? – Claudio May 05 '17 at 07:54
  • Why are you xor'ing the key with data? It this homework, if not what are you trying to achieve, CBC mode? – zaph May 05 '17 at 16:23
  • I've already solved it I had to implement AES CBC with an existing AES, the thing is the IV was as str and the rest of the data was in bytes "normal" xoring didn't work and the decrypting was fine on the library implementation but not on mine, the name key and data are arbitrary in this case since in cbc you need to xor between iv and D(c) to get the original block. nvm I've already solved it so it doesn't really matters – LiorA May 06 '17 at 12:14
  • and the CBC was just to bring it to context and not the main part of the question – LiorA May 06 '17 at 12:16

1 Answers1

0

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
LiorA
  • 441
  • 3
  • 13