xoring bytes and str(AES CBC)

问题描述:

嗨我真的很感谢这个帮助,因为我真的迷失了,我不明白为什么它不起作用。xoring bytes and str(AES CBC)

我有一个16个字节的密钥和16字节块 但关键类型是“海峡”和块类型是“字节”,我希望他们之间异或的,但价值是不正确的(我认为) 验证码基于this

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) 

,并在上下文中:(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) 

,并在这里,我所有写入文件

# 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'') 

我已经试过关于XOR(因为ECB解密由库pycrypto制造)等之类的东西用它number.bytes长和XOR运算是 ,我已经试图将其转换为整数,并看看它是怎么回事,一切都变得非常糟糕 和xor的事情只是一个预感我不能真正躺在我的手指为什么这不工作正常! 感谢所有的帮手

+0

能详细显示什么是您认为不正确,什么是不正常和正确的结果应该看起来怎么样? – Claudio

+0

你为什么在数据上留下钥匙?这个作业,如果不是你想要达到什么,CBC模式? – zaph

+0

我已经解决了它,我必须用现有的AES实现AES CBC,事情是IV是str,其余的数据是字节“正常”xoring不起作用,解密没有问题库的实现,但不是我的,在这种情况下,名称密钥和数据是任意的,因为在cbc中,您需要在iv和D(c)之间进行异或以获得原始块。 nvm我已经解决了它,所以它不是真的很重要 – LiorA

我已经设法解决它! 问题确实与XOR函数,因为我认为,显然XOR结果的长度不是16,但由于转换它是变种(为什么是,虽然?)

我添加代码这里柜面任何人都需要在未来

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