-
Notifications
You must be signed in to change notification settings - Fork 0
/
DES - ECB.py
103 lines (67 loc) · 3.4 KB
/
DES - ECB.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
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
#19.DES with Avalanche Effect
def des_encrypt(data, key):
max_len = max(len(data), len(key))
data = data.ljust(max_len, '\x00')
key = key.ljust(max_len, '\x00')
data_bin = ''.join(format(ord(char), '08b') for char in data)
key_bin = ''.join(format(ord(char), '08b') for char in key)
encrypted_data_bin = ''.join(str(int(data_bit) ^ int(key_bit)) for data_bit, key_bit in zip(data_bin, key_bin))
encrypted_data = ''.join(chr(int(encrypted_data_bin[i:i+8], 2)) for i in range(0, len(encrypted_data_bin), 8))
return encrypted_data
data = input("Enter the mssg: ")
key = "BISCUIT6677"
encrypted_data = des_encrypt(data, key)
print("Original Data:", data)
print("Encrypted Data:", encrypted_data)
#20.Electronic codebook mode with Avalanche Effect
import random
def ecb_encrypt_with_avalanche(data, key):
max_len = max(len(data), len(key))
data = data.ljust(max_len, '\x00')
key = key.ljust(max_len, '\x00')
data_bin = ''.join(format(ord(char), '08b') for char in data)
key_bin = ''.join(format(ord(char), '08b') for char in key)
encrypted_data_bin = ""
block_size = 64
for i in range(0, len(data_bin), block_size):
block = data_bin[i:i + block_size]
random_bits = ''.join(str(random.randint(0, 1)) for _ in range(block_size))
mixed_block = ''.join(str(int(data_bit) ^ int(random_bit)) for data_bit, random_bit in zip(block, random_bits))
encrypted_block = ''.join(str(int(mixed_bit) ^ int(key_bit)) for mixed_bit, key_bit in zip(mixed_block, key_bin))
encrypted_data_bin += encrypted_block
encrypted_data = ''.join(chr(int(encrypted_data_bin[i:i + 8], 2)) for i in range(0, len(encrypted_data_bin), 8))
return encrypted_data
data = input("Enter the mssg: ")
key = "CUTIE4567"
encrypted_data = ecb_encrypt_with_avalanche(data, key)
print("Original Data:", data)
print("Encrypted Data with Avalanche Effect (ECB):", encrypted_data)
#21.Cipher block chaining mode with avalanche effect
import random
def zero_pad_binary_string(binary_str, length):
if len(binary_str) < length:
return '0' * (length - len(binary_str)) + binary_str
return binary_str
def xor_binary_strings(str1, str2):
return ''.join(str(int(bit1) ^ int(bit2)) for bit1, bit2 in zip(str1, str2))
def cbc_encrypt_with_avalanche(data, key, iv):
data_bin = ''.join(format(ord(char), '08b') for char in data)
key_bin = ''.join(format(ord(char), '08b') for char in key)
iv_bin = ''.join(format(ord(char), '08b') for char in iv)
data_bin = zero_pad_binary_string(data_bin, len(key_bin))
iv_bin = zero_pad_binary_string(iv_bin, len(key_bin))
ciphertext = ""
previous_block = xor_binary_strings(data_bin[:len(key_bin)], iv_bin)
for i in range(0, len(data_bin), len(key_bin)):
current_block = data_bin[i:i+len(key_bin)]
mixed_block = xor_binary_strings(current_block, previous_block)
encrypted_block = xor_binary_strings(mixed_block, key_bin)
ciphertext += encrypted_block
previous_block = encrypted_block
return ciphertext
data = input("Enter the mssg : ")
key = "SecretKey12345"
iv = "Initialization"
encrypted_data = cbc_encrypt_with_avalanche(data, key, iv)
print("Original Data:", data)
print("Encrypted Data with Avalanche Effect (CBC):", encrypted_data)