Skip to content

Commit e7570d8

Browse files
committed
MGJW: this week | 信标定长(研究)
大约从上午十点多写到了下午2点半,待会儿要去开组会了 Signed-off-by: LetMeFly666 <[email protected]>
1 parent d70b2f0 commit e7570d8

File tree

6 files changed

+571
-1
lines changed

6 files changed

+571
-1
lines changed

tryGoPy/MGJW/thisWeek/chat.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!--
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-20 11:00:50
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-20 14:08:53
6+
-->
7+
使用一段人类可读的描述,以一个本科生写周报的口吻,一段话详细介绍AES-CBC加密原文和密文之间的长度关系。
8+
要求:
9+
1. 凸显研究过程(探究、计算、得出结论等)
10+
2. 可以包含公式
11+
3. 以人类口吻而非机器口吻,不要列举一二三条
12+
13+
---
14+
15+
假设信标中每个字符由数字和字母组成(10+26=36)种,那么信标长度为$n$的情况下,有$k$封邮件随机生成的信标有重合的概率为 多少?
16+
17+
18+
很棒!返回文字和公式的latex源码。
19+
20+
21+
假设邮件有$10^{16}$封(邮件服务商提供预计可以提供1亿亿次邮件信标服且不会发生冲突),则信标长度至少要为多少才有极大概率得以保证?允许出现邮件信标冲突的概率为亿亿分之1。
22+
23+
24+
很棒!返回文字和公式的latex源码。
25+
26+
---
27+
28+
我使用AES加密后得到密文,如果我将密文以16进制形式显示的话,实际长度似乎要翻倍。请你设计一个较好的展示方法,目标是人类可读字符,长度尽可能地短。
29+
30+
31+
base64编码后长度能确定吗?长度和原文是什么关系?
32+
33+
---
34+
35+
请将一个字符串分别以AES不同方法加密,比较密文长度。
36+
37+
写一个python脚本来展示。
38+
39+
40+
AES129 194 256的区别呢
41+
42+
---
43+
44+
以本科生的口吻在周报里讲base64编码,侧重原文长度和编码后的长度的关系。使用一段式描述,以人类口吻而非机器口吻,不要列举一二三条。
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-04-20 11:01:24
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-20 11:13:19
6+
'''
7+
from Crypto.Cipher import AES
8+
from Crypto.Util.Padding import pad, unpad
9+
10+
11+
def encrypt_aes(plaintext: str, key: bytes) -> bytes:
12+
cipher = AES.new(key, AES.MODE_CBC) # 使用CBC模式
13+
ct_bytes = cipher.encrypt(pad(plaintext.encode(), AES.block_size))
14+
return cipher.iv + ct_bytes # 返回IV + 密文
15+
16+
msg = '其实今天要进攻'
17+
key = bytes.fromhex('e3a9a39b3c95c109257aeb8c09cb8ad331779647c54de1a40a66d308f8e4a882') # 演示用,实际可别公布
18+
encrypted = encrypt_aes(msg, key)
19+
20+
print(encrypted.hex())
21+
print(len(msg), '->', len(encrypted.hex()))
22+
# decrypt = decrypt_aes(encrypted, key)
23+
# print(decrypt)
24+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-04-20 11:01:24
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-20 12:10:02
6+
'''
7+
from Crypto.Cipher import AES
8+
from Crypto.Util.Padding import pad, unpad
9+
import random
10+
11+
12+
def encrypt_aes(plaintext: bytes, key: bytes) -> bytes: # to后人:这次随机生成的直接是bytes
13+
cipher = AES.new(key, AES.MODE_CBC)
14+
ct_bytes = cipher.encrypt(pad(plaintext, AES.block_size)) # 因为plaintext是bytes类型,所以这里删掉了encode
15+
return cipher.iv + ct_bytes
16+
17+
def encryptOnce(msg: bytes):
18+
encrypted = encrypt_aes(msg, key)
19+
# print(encrypted.hex())
20+
print(len(msg), '->', len(encrypted.hex()))
21+
22+
23+
key = bytes.fromhex('e3a9a39b3c95c109257aeb8c09cb8ad331779647c54de1a40a66d308f8e4a882') # 演示用,实际可别公布
24+
for originalLength in range(100):
25+
msg = random.randbytes(originalLength)
26+
encryptOnce(msg)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-04-20 12:57:49
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-20 12:57:51
6+
'''
7+
import base64
8+
9+
# AES加密后的原始字节
10+
ciphertext = b'\x4d\x61\x6e\x79\x20\x73\x74\x72\x69\x6e\x67\x73'
11+
12+
# 转换为Base64
13+
base64_str = base64.b64encode(ciphertext).decode('ascii')
14+
print(base64_str) # 输出: TWlueSBzdHJpbmdz
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-04-20 13:42:49
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-20 14:07:03
6+
'''
7+
from Crypto.Cipher import AES
8+
from Crypto.Util.Padding import pad, unpad
9+
from Crypto.Random import get_random_bytes
10+
import base64
11+
12+
def aes_encrypt(plaintext, mode, key, iv=None):
13+
if mode == AES.MODE_ECB:
14+
cipher = AES.new(key, AES.MODE_ECB)
15+
elif mode == AES.MODE_CBC:
16+
cipher = AES.new(key, AES.MODE_CBC, iv)
17+
elif mode == AES.MODE_CFB:
18+
cipher = AES.new(key, AES.MODE_CFB, iv)
19+
elif mode == AES.MODE_OFB:
20+
cipher = AES.new(key, AES.MODE_OFB, iv)
21+
else:
22+
return
23+
24+
padded_text = pad(plaintext.encode('utf-8'), AES.block_size)
25+
ciphertext = cipher.encrypt(padded_text)
26+
return ciphertext
27+
28+
def compare_aes_modes(plaintext):
29+
key = get_random_bytes(16) # 目标是加密结果尽可能少,所以选择AES128而不是192或256
30+
iv = get_random_bytes(16)
31+
32+
modes = [
33+
("ECB", AES.MODE_ECB),
34+
("CBC", AES.MODE_CBC),
35+
("CFB", AES.MODE_CFB),
36+
("OFB", AES.MODE_OFB)
37+
]
38+
39+
print(plaintext)
40+
41+
results = []
42+
for name, mode in modes:
43+
# ECB模式不需要IV
44+
iv_param = iv if mode != AES.MODE_ECB else None
45+
ciphertext = aes_encrypt(plaintext, mode, key, iv_param)
46+
47+
ciphertext_length = len(ciphertext)
48+
base64_length = len(base64.b64encode(ciphertext))
49+
50+
results.append((name, ciphertext_length, base64_length))
51+
52+
print(f"\n{name}")
53+
print(f"密文长度: {ciphertext_length}")
54+
print(f"Base64编码后长度: {base64_length}")
55+
print(f"Base64密文: {base64.b64encode(ciphertext).decode('utf-8')}")
56+
57+
58+
plaintext = "test test test test test test handsome li test "
59+
print(len(plaintext))
60+
compare_aes_modes(plaintext)

0 commit comments

Comments
 (0)