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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# ================= GF(2^8) =================
def gf_mul(a, b):
res = 0
for _ in range(8):
if b & 1:
res ^= a
hi = a & 0x80
a = (a << 1) & 0xFF
if hi:
a ^= 0x1B
b >>= 1
return res
def gf_pow254(x):
if x == 0:
return 0
y = 1
for _ in range(253):
y = gf_mul(y, x)
return y
def rol8(x, n):
return ((x << n) | (x >> (8-n))) & 0xFF
# ================= 构建 SBox 和逆 SBox =================
def build_sbox():
s = []
for i in range(256):
inv = gf_pow254(i)
y = inv ^ rol8(inv,1) ^ rol8(inv,2) ^ rol8(inv,3) ^ rol8(inv,4)
y ^= 0x30
s.append(y)
return s
SBOX = build_sbox()
INV_SBOX = [0]*256
for i, val in enumerate(SBOX):
INV_SBOX[val] = i
# ================= key schedule =================
RCON = [ord(c) for c in "easypython?"] # 11 bytes
def key_expand(key16: bytes):
w = list(key16)
for j in range(4, 44):
t0, t1, t2, t3 = w[4*j-4:4*j]
if j % 4 == 0:
t0, t1, t2, t3 = t1, t2, t3, t0
t0, t1, t2, t3 = SBOX[t0], SBOX[t1], SBOX[t2], SBOX[t3]
t0 ^= RCON[j//4]
for k, t in enumerate([t0, t1, t2, t3]):
w.append(w[4*(j-4)+k] ^ t)
return w # 176 bytes
# ================= AES 基本操作 =================
def add_round_key(round_id, state, rk):
for i in range(4):
for j in range(4):
state[4*i+j] ^= rk[16*round_id + 4*i + j]
def sub_bytes(state):
for i in range(4):
for j in range(4):
state[4*j+i] = SBOX[state[4*j+i]]
def inv_sub_bytes(state):
for i in range(4):
for j in range(4):
state[4*j+i] = INV_SBOX[state[4*j+i]]
def shift_rows(s):
s[1], s[5], s[9], s[13] = s[5], s[9], s[13], s[1]
s[2], s[10] = s[10], s[2]
s[6], s[14] = s[14], s[6]
s[3], s[15], s[11], s[7] = s[15], s[11], s[7], s[3]
def inv_shift_rows(s):
# 第1行右移1 (即左移3)
s[1], s[5], s[9], s[13] = s[13], s[1], s[5], s[9]
# 第2行右移2 (自逆)
s[2], s[10] = s[10], s[2]
s[6], s[14] = s[14], s[6]
# 第3行右移1 (即左移1)
s[3], s[7], s[11], s[15] = s[7], s[11], s[15], s[3]
def xtime(x):
return ((x<<1) & 0xFF) ^ (0x1B if (x & 0x80) else 0)
def mix_columns(s):
for i in range(4):
a0, a1, a2, a3 = s[4*i:4*i+4]
t = a0 ^ a1 ^ a2 ^ a3
u = a0
s[4*i+0] ^= t ^ xtime(a0 ^ a1)
s[4*i+1] ^= t ^ xtime(a1 ^ a2)
s[4*i+2] ^= t ^ xtime(a2 ^ a3)
s[4*i+3] ^= t ^ xtime(a3 ^ u)
def inv_mix_columns(s):
for i in range(4):
a0, a1, a2, a3 = s[4*i:4*i+4]
b0 = gf_mul(a0, 0x0e) ^ gf_mul(a1, 0x0b) ^ gf_mul(a2, 0x0d) ^ gf_mul(a3, 0x09)
b1 = gf_mul(a0, 0x09) ^ gf_mul(a1, 0x0e) ^ gf_mul(a2, 0x0b) ^ gf_mul(a3, 0x0d)
b2 = gf_mul(a0, 0x0d) ^ gf_mul(a1, 0x09) ^ gf_mul(a2, 0x0e) ^ gf_mul(a3, 0x0b)
b3 = gf_mul(a0, 0x0b) ^ gf_mul(a1, 0x0d) ^ gf_mul(a2, 0x09) ^ gf_mul(a3, 0x0e)
s[4*i:4*i+4] = [b0, b1, b2, b3]
def aes_encrypt_block(state, w):
add_round_key(0, state, w)
for r in range(1, 10):
sub_bytes(state)
shift_rows(state)
mix_columns(state)
add_round_key(r, state, w)
sub_bytes(state)
shift_rows(state)
add_round_key(10, state, w)
def aes_decrypt_block(state, w):
add_round_key(10, state, w)
for r in range(9, 0, -1):
inv_shift_rows(state)
inv_sub_bytes(state)
add_round_key(r, state, w)
inv_mix_columns(state)
inv_shift_rows(state)
inv_sub_bytes(state)
add_round_key(0, state, w)
# ================= 解密主函数 =================
KEY = b"nice_to_meet_you"
IV = b"1145141145144332"
def decrypt_48(cipher_hex: str):
cipher = bytes.fromhex(cipher_hex)
assert len(cipher) == 48
w = key_expand(KEY)
iv = list(IV) # 初始IV
plain = bytearray()
for i in range(0, 48, 16):
block = list(cipher[i:i+16])
state = block[:] # 密文块
aes_decrypt_block(state, w) # state 现在为中间值 = 明文 ^ iv ^ 0x33
# 恢复明文
for j in range(16):
state[j] ^= iv[j] ^ 0x33
plain.extend(state)
iv = block # 下一个IV用当前密文块(原始密文)
return bytes(plain)
# ================= 执行解密 =================
if __name__ == "__main__":
cipher_hex = "4245E97D2232F72C3DCA15F74FCC844BDEADB951CF29CDE2336060A62C034F551174A4D805F4AC44BA204B8600269074"
plain = decrypt_48(cipher_hex)
print("解密结果(十六进制):", plain.hex())
try:
print("解密结果(UTF-8):", plain.decode('utf-8'))
except:
print("解密结果(原始字节):", plain)
|