抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

OFB Mode

img

img


OFB模式的全称是output-Feedback模式(输出反馈模式)。在OFB模式中,密码算法的输出会反馈到密码算法的输入中。

OFB模式不是通过密码算法对明文直接加密的,而是通过将“明文分组”和“密码算法的输出”进行XOR来产生“密文分组”的。


另一种看的比较清楚的图解如下:

先是加密模式:

img

然后是解密模式:

img

欸,乍一看是不是俩张图没有什么区别!?

所以很自然的想到把密文放入加密模式,那岂不是K1就会和密文1进行xor异或运算了?

这不正好是我们的解密过程吗家人们!!!!

接下来cryptohack上引入一道题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from Crypto.Cipher import AES


KEY = ?
FLAG = ?


@chal.route('/symmetry/encrypt/<plaintext>/<iv>/')
def encrypt(plaintext, iv):
plaintext = bytes.fromhex(plaintext)
iv = bytes.fromhex(iv)
if len(iv) != 16:
return {"error": "IV length must be 16"}

cipher = AES.new(KEY, AES.MODE_OFB, iv)
encrypted = cipher.encrypt(plaintext)
ciphertext = encrypted.hex()

return {"ciphertext": ciphertext}


@chal.route('/symmetry/encrypt_flag/')
def encrypt_flag():
iv = os.urandom(16)

cipher = AES.new(KEY, AES.MODE_OFB, iv)
encrypted = cipher.encrypt(FLAG.encode())
ciphertext = iv.hex() + encrypted.hex()

return {"ciphertext": ciphertext}

先通过request = request.get('https://aes.cryptohack.org/symmetry/encrypt_flag/')访问到密文,根据源码可知ciphertext的前16个字节为iv,我们可以先通过json = request.json()['ciphertext‘获取到密文内容,然后通过iv = json[:32]获取到iv的十六进制格式,那其余的部分就是密文的部分,将密文带入到加密算法中,就可以获取到明文了

以下为解题代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
# cca2ef4345b833fe55f35262d6ba6d227bc99ee91a50de20105c0966276262fc311bf674075a58e4672b3e43d8c66c3fd1

import requests

result = requests.get('https://aes.cryptohack.org/symmetry/encrypt_flag/')
ciphertext = result.json()["ciphertext"]
iv = ciphertext[0:32]
c = ciphertext[32:]
print(iv, c)

result = requests.get('https://aes.cryptohack.org/symmetry/encrypt/' + c + '/' + iv)
ciphertext = result.json()["ciphertext"]
print(bytes.fromhex(ciphertext))

OFB算是比较简单的AES类型,因为其加密解密方式是一模一样的

评论