【密码学】 字节反转攻击

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’

  • 25
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值