IDEA加密算法

前言

觉得手算一轮这个IDEA加密太不优雅,就用python解放下双手了= =

脚本

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
# -- coding: utf-8 --
def mod_65536_plus(a, b):
return (a + b) % 65536


def mod_65537_mult(a, b):
return (a * b) % 65537


def left_25(lst):
return lst[25:] + lst[:25]


s = "00000000111111110000000011111111111111110000111111110000111111110000111111110000111111110000000000001111111111111111000000001111"
s1 = left_25(s)
# s1 = 1111111111111110 0001111111100001 1111111000011111 1110000111111110 0000000000011111 1111111111100000 0001111000000001 1111111000000001
# 例题M = [0, 0b1010101011100110, 0b0101010100001111, 0b1100110000110011, 0b1001100101100110]
M = [4,
0b0110010000011111,
0b1001010100101101,
0b0000101001000010,
0b1000000001111111]
K0 = [8,
0b0000000011111111,
0b0000000011111111,
0b1111111100001111,
0b1111000011111111,
0b0000111111110000,
0b1111111100000000,
0b0000111111111111,
0b1111000000001111]
K1 = []
for i in range(8):
K1.append(int(s1[16*i:16*(i+1)], 2))

r1 = mod_65537_mult(M[1], K0[7])
r2 = mod_65536_plus(M[2], K0[8])
r3 = mod_65536_plus(M[3], K1[1])
r4 = mod_65537_mult(M[4], K1[2])
r5 = r1 ^ r3
r6 = r2 ^ r4
r7 = mod_65537_mult(r5, K1[3])
r8 = mod_65536_plus(r7, r6)
r9 = mod_65537_mult(r8, K1[4])
r10 = mod_65536_plus(r7, r9)
r11 = r1 ^ r9
r12 = r2 ^ r10
r13 = r3 ^ r9
r14 = r4 ^ r10


print(bin(r1)[2:].rjust(16, '0'))
print(bin(r2)[2:].rjust(16, '0'))
print(bin(r3)[2:].rjust(16, '0'))
print(bin(r4)[2:].rjust(16, '0'))
print(bin(r5)[2:].rjust(16, '0'))
print(bin(r6)[2:].rjust(16, '0'))
print(bin(r7)[2:].rjust(16, '0'))
print(bin(r8)[2:].rjust(16, '0'))
print(bin(r9)[2:].rjust(16, '0'))

output = [bin(r11)[2:].rjust(16, '0'),
bin(r13)[2:].rjust(16, '0'),
bin(r12)[2:].rjust(16, '0'),
bin(r14)[2:].rjust(16, '0')]
print(output)

小结

时间不是很充裕,大概就是实现一个轮结构,主要是交作业用。

0%