python - First 8 byes of my encrypted data corrupting using 3DES and CBC -
i'm using pycrypto in application encrypt data, reason first 8 bytes (corresponding first block) coming through corrupt no matter do.
>>> crypto.cipher import des3 >>> crypto import random >>> iv = random.new().read(des3.block_size) >>> key = random.new().read(des3.key_size[-1]) >>> des3 = des3.new(key, des3.mode_cbc, iv) >>> des3.decrypt(des3.encrypt('12345678abcdefgh12345678')) 't\x1b\x0f\xcbd\x15m\xababcdefgh12345678'
i've read that's sign iv corrupt, sources using mode other cbc result in entire message corrupting. isn't case:
>>> des3 = des3.new(key, des3.mode_cfb, iv) >>> des3.decrypt(des3.encrypt('12345678abcdefgh12345678')) '\xe1\x85\xae,\xf1m\x83\x9cabcdefgh12345678'
i can rule out cipher cause:
>>> crypto.cipher import aes >>> crypto import random >>> iv = random.new().read(aes.block_size) >>> key = random.new().read(aes.key_size[-1]) >>> aes = aes.new(key, aes.mode_cbc, iv) >>> aes.decrypt(aes.encrypt('12345678abcdefgh12345678abcdefgh')) '\xa7l\x00]\x1cw\xec\xd0\x04\x06\xba&\x1663\xd712345678abcdefgh'
note in example first 16 bytes corrupt, corresponds aes' block size.
you have reset iv vector before decryption. try code:
>>> crypto.cipher import des3 >>> crypto import random >>> iv = random.new().read(des3.block_size) >>> key = random.new().read(des3.key_size[-1]) >>> des3enc = des3.new(key, des3.mode_cbc, iv) >>> des3dec = des3.new(key, des3.mode_cbc, iv) >>> des3dec.decrypt(des3enc.encrypt('12345678abcdefgh12345678'))
iv vector changing after encryption / decryption each block. used same instance of des3 class encrypting , decrypting message, therefore had incorrect iv decryption.
hope above code works - didn't test it.
more cbc mode: http://en.wikipedia.org/wiki/block_cipher_mode_of_operation
Comments
Post a Comment