Skip to content

Commit 72b2652

Browse files
committed
Added file encryption tests for ECB and CBC
1 parent 05035c4 commit 72b2652

File tree

4 files changed

+162
-1
lines changed

4 files changed

+162
-1
lines changed

hight_ECB.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def ecb_hight_encryption(P, MK):
2929
def ecb_hight_decryption(C, MK):
3030
WK, SK = decryption_key_schedule(MK)
3131
D = decryption_transformation(C, WK, SK)
32-
for block in range(8, len(P), 8):
32+
for block in range(8, len(C), 8):
3333
D += decryption_transformation(C[block:block + 8], WK, SK)
3434
return D
3535

test.txt

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
1
2+
2
3+
3
4+
4
5+
5
6+
6
7+
7
8+
8
9+
9
10+
10
11+
11
12+
12
13+
13
14+
14
15+
15
16+
16
17+
17
18+
18
19+
19
20+
20
21+
21
22+
22
23+
23
24+
24
25+
25
26+
26
27+
27
28+
28
29+
29
30+
30
31+
31
32+
32
33+
33
34+
34
35+
35
36+
36
37+
37
38+
38
39+
39
40+
40
41+
41
42+
42
43+
43
44+
44
45+
45
46+
46
47+
47
48+
48
49+
49
50+
50
51+
51
52+
52
53+
53
54+
54
55+
55
56+
56
57+
57
58+
58
59+
59
60+
60
61+
61
62+
62
63+
63
64+
64
65+
65
66+
66
67+
67
68+
68
69+
69
70+
70
71+
71
72+
72
73+
73
74+
74
75+
75
76+
76
77+
77
78+
78
79+
79
80+
80
81+
81
82+
82
83+
83
84+
84
85+
85
86+
86
87+
87
88+
88
89+
89
90+
90
91+
91
92+
92
93+
93
94+
94
95+
95
96+
96
97+
97
98+
98
99+
99
100+
100

test_CBC.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import io
2+
import sys
3+
sys.stdout, temp = io.StringIO(), sys.stdout
4+
from hight_CBC import cbc_hight_encryption, cbc_hight_decryption
5+
sys.stdout = temp
6+
7+
# Similar to how Crypto.Util.Padding is implemented
8+
def pad(byte_list_to_pad: list, block_size: int):
9+
assert byte_list_to_pad[-1] != 13
10+
return byte_list_to_pad + list(b"\r") * (-len(byte_list_to_pad) % block_size)
11+
12+
def unpad(padded_byte_list: list):
13+
while padded_byte_list and padded_byte_list[-1] == 13:
14+
del padded_byte_list[-1]
15+
return padded_byte_list
16+
17+
# TEST CASE
18+
MK = [0x88, 0xE3, 0x4F, 0x8F, 0x08, 0x17, 0x79, 0xF1, 0xE9, 0xF3, 0x94, 0x37, 0x0A, 0xD4, 0x05, 0x89]
19+
IV = [0x26, 0x8D, 0x66, 0xA7, 0x35, 0xA8, 0x1A, 0x81]
20+
P = list(open("test.txt", "rb").read())
21+
22+
# MAIN CODE
23+
print("Plaintext:", [hex(byte)[2:].upper() for byte in P])
24+
25+
C = cbc_hight_encryption(pad(P, 8), IV, MK)
26+
print("Encrypted bytes:", [hex(byte)[2:].upper() for byte in C])
27+
28+
D = unpad(cbc_hight_decryption(C, IV, MK))
29+
print("Decrypted bytes:", [hex(byte)[2:].upper() for byte in D])
30+
31+
assert D == P

test_ECB.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import io
2+
import sys
3+
sys.stdout, temp = io.StringIO(), sys.stdout
4+
from hight_ECB import ecb_hight_encryption, ecb_hight_decryption
5+
sys.stdout = temp
6+
7+
# Similar to how Crypto.Util.Padding is implemented
8+
def pad(byte_list_to_pad: list, block_size: int):
9+
assert byte_list_to_pad[-1] != 13
10+
return byte_list_to_pad + list(b"\r") * (-len(byte_list_to_pad) % block_size)
11+
12+
def unpad(padded_byte_list: list):
13+
while padded_byte_list and padded_byte_list[-1] == 13:
14+
del padded_byte_list[-1]
15+
return padded_byte_list
16+
17+
# TEST CASE
18+
MK = [0x88, 0xE3, 0x4F, 0x8F, 0x08, 0x17, 0x79, 0xF1, 0xE9, 0xF3, 0x94, 0x37, 0x0A, 0xD4, 0x05, 0x89]
19+
P = list(open("test.txt", "rb").read())
20+
21+
# MAIN CODE
22+
print("Plaintext:", [hex(byte)[2:].upper() for byte in P])
23+
24+
C = ecb_hight_encryption(pad(P, 8), MK)
25+
print("Encrypted bytes:", [hex(byte)[2:].upper() for byte in C])
26+
27+
D = unpad(ecb_hight_decryption(C, MK))
28+
print("Decrypted bytes:", [hex(byte)[2:].upper() for byte in D])
29+
30+
assert D == P

0 commit comments

Comments
 (0)