-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIDEA.py
64 lines (35 loc) · 1.33 KB
/
IDEA.py
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
def idea_encrypt(plaintext, key):
round_keys = generate_round_keys(key)
plaintext_blocks = [plaintext[i:i + 8] for i in range(0, len(plaintext), 8)]
ciphertext_blocks = []
for block in plaintext_blocks:
block = pad_block(block)
data = bytes_to_int(block)
data = initial_permutation(data)
for round_key in round_keys:
data = round_encrypt(data, round_key)
data = final_permutation(data)
ciphertext_blocks.append(int_to_bytes(data))
return b''.join(ciphertext_blocks)
def generate_round_keys(key):
round_keys = []
for i in range(8):
round_keys.append(bytes_to_int(key[i * 8:(i + 1) * 8]))
return round_keys
def pad_block(block):
return block + b'\x00' * (8 - len(block))
def bytes_to_int(data):
return int.from_bytes(data, byteorder='big')
def int_to_bytes(data):
return data.to_bytes(8, byteorder='big')
def initial_permutation(data):
return data
def final_permutation(data):
return data
def round_encrypt(data, round_key):
return data ^ round_key
plaintext = b'LIfe is too harsh to live so uyou have to grind.'
key = b'SecretKey'
ciphertext = idea_encrypt(plaintext, key)
print("Original Plaintext:", plaintext)
print("Ciphertext:", ciphertext)