CBC字节反转攻击
CBC字节反转攻击,示例代码如下:
from Crypto. Cipher import AES
from os import urandom
from Crypto.Util.strxor import strxor
class AES_CBC:
def __init__(self):
self.key = urandom(16)
self.iv = urandom(16)
print(self.key)
print(self.iv)
def encrypt(self, plain):
aes = AES.new(self.key, AES.MODE_CBC, self.iv)
return aes.encrypt(plain)
def decrypt(self, cipher):
aes = AES.new(self.key, AES.MODE_CBC, self.iv)
return aes.decrypt(cipher)
plain = b'1'*32
aes = AES_CBC()
cipher = aes.encrypt(plain)
print (aes.decrypt(cipher))
//字符反转
cipher = strxor(strxor(cipher[:16], b'1'*16), b'2'*16)+cipher [16:]
print (aes.decrypt(cipher))
运行后结果如下:
b’&\xc4\xbf%\xa0F\xe0\x0cN\x8dt\x98\xf6PL\x7f’
b’\xa5S\x06\x88\x83s\xa5\xa9\xa2K\xae\xc6\xff\xf8)\xa9’
b’11111111111111111111111111111111’
b’(U\x0e&j\xf2\t\xf9\x1d6\xb5\xaaB$mw2222222222222222’
CFB字符反转
from Crypto.Cipher import AES
from os import urandom
from Crypto. Util.strxor import strxor
class AES_CFB:
def __init__ (self):
self.key = urandom(16)
self.iv = urandom(16)
def encrypt(self, plain):
aes = AES.new(self.key, AES.MODE_CFB, self.iv)
return aes.encrypt(plain)
def decrypt(self, cipher):
aes = AES.new(self.key, AES.MODE_CFB, self.iv)
return aes.decrypt (cipher)
plain = b'1'*32
aes = AES_CFB()
cipher = aes.encrypt(plain)
print( aes.decrypt (cipher))
for i in range(32):
pt =aes.decrypt(cipher)
cipher = cipher[:i]+ strxor(strxor(cipher[i:i+1], pt[i:i+1]), b'2')+cipher[i+1:]
print( aes.decrypt(cipher))
运行结果如下
b’11111111111111111111111111111111’
b’2 \xa8\x89V\xab\xae\x0b\x041C(\xe5rn\xba\x9d111111111111111’
b’22\xe7G\xef\xab\xcc\x82\x1c\xd7\xfd!\x0b\xaep\xb3\xca\xa911111111111111’
b’222^\xc1\xf6;e\x17\x1a\x02KaTeX parse error: Undefined control sequence: \xf at position 1: \̲x̲f̲5]"S,`\xe711111…\x9cL&\x94:R1’
b’2222222222222222\xb2\xdf\xe8\xec%\xb2]\xc4%{)\xd4\x05\x02=\xd8’
b’22222222222222222S\x1a\x92\xdfW\xb5\xd1\xcd\x88\xa1\x93\xe0\x99^\xf2’
b’222222222222222222\xdf;\xf2\t\xea\xc7\x1a\xee\t\p\x89\x7f\xb1’
b’2222222222222222222\xbb7^U\x14\x9b\xbb-\xabC\x82\x15\x97’
b’22222222222222222222n\t\d\xb1;^\x15\x06\xc4\xc9\x93’
b"2222222222222222222224\x98\x81\xc9y%\xb8\xe4’\xa4D"
b"2222222222222222222222J\xc4\xc0\xa1\x1f’I\xac\xbf\x8a"
b’22222222222222222222222\xb6y\xaa\x9d<f\xb6D\xf3’
b’222222222222222222222222\xf7\xf2\xb5\x93\xd0wP\xd6’
b’2222222222222222222222222D\xd0\xa6\xba\xfd\xea@’
b’22222222222222222222222222u\xfb\xe5"\xed\x84’
b’222222222222222222222222222Q\x7fjY\x07’
b’2222222222222222222222222222.\xaa\x13\xf0’
b’22222222222222222222222222222\xe3\xf4\xb7’
b’222222222222222222222222222222\x83\x82’
b’2222222222222222222222222222222\xab’
b’22222222222222222222222222222222’
OFB字符反转攻击
from Crypto.Cipher import AES
from os import urandom
from Crypto. Util.strxor import strxor
class AES_OFB:
def __init__ (self):
self.key = urandom(16)
self.iv = urandom(16)
def encrypt(self, plain):
aes = AES.new(self.key, AES.MODE_OFB, self.iv)
return aes.encrypt(plain)
def decrypt(self, cipher):
aes = AES.new(self.key, AES.MODE_OFB, self.iv)
return aes.decrypt (cipher)
plain = b'1'*32
aes = AES_OFB()
cipher = aes.encrypt(plain)
print( aes.decrypt (cipher))
for i in range(32):
ct = strxor(strxor(cipher[:i+1], b'1'*(i+1)), b'2'*(i+1))+cipher[i+1:]
print( aes.decrypt(ct))
运行结果如下:
b’11111111111111111111111111111111’
b’22222222222222222222222222222222’