2018/05/30

[Cryptography] 複習筆記: Week4 - Active attack on CBC encryption with a random IV using AES

本週課程探討AE(Authenticated Encryption)的重要性,因為不論是流加密(Stream cipher)或區塊加密(Block cipher),雖然是CPA(Cipher plaintext attack) security,但是無法阻擋Active attack在資料上的完整(Integrity)。


這次題目就以CBC在Random IV之下的AES加密,只要修改IV(initial vector),就能夠達到竄改資料的目的。舉例來說:

我們攔截到一段祕文(Ciphertext):
20814804c1767293b99f1d9cab3bc3e7 ac1e37bfb15599e5f40eef805488281d

且我們知道在明文(Plaintext)的部分是:
Pay Bob 100$

想要改成:
Pay Bob 500$

在做法上只要把IV的部份,按照講義的概念
IV' = IV xor (Pay Bob 100$) xor (Pay Bob 500$)
就能夠達到目的了。

怎麼做,首先要先計算(Pay Bob 100$) xor (Pay Bob 500$),然後再把其結果與IV做運算,最後就可以得到竄改後的IV'。

Python程式碼:
#!/usr/bin/env python
def strxor(a, b):     # xor two strings of different lengths
    if len(a) > len(b):
       return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])
    else:
       return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])])

block_size = 16
IV_prime = '0000000000000000'
ciphertext = '20814804c1767293b99f1d9cab3bc3e7 ac1e37bfb15599e5f40eef805488281d'
plaintext = 'Pay Bob 100$'
tampertext = 'Pay Bob 500$'

xor_IV = strxor(plaintext, tampertext)
print('xor_IV: {}, len: {}'.format(xor_IV.encode('hex'), len(xor_IV)))

if len(xor_IV) < 32:
    xor_IV = xor_IV + '\x00' * (block_size-len(xor_IV))
    print('xor_IV: {}, len: {}'.format(xor_IV.encode('hex'), len(xor_IV)))

IV = ciphertext[:2*block_size]
print('IV: {}, len: {}'.format(IV, len(IV)))

xor_cipher = strxor(IV.decode('hex'), xor_IV)

result_cipher = xor_cipher.encode('hex') + ciphertext[2*block_size:]
print('result_cipher: {}'.format(result_cipher))


結果:

就可以得到答案,'20814804c1767293bd9f1d9cab3bc3e7 ac1e37bfb15599e5f40eef805488281d'。

沒有留言:

張貼留言