-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_main.py
More file actions
135 lines (77 loc) · 2.42 KB
/
final_main.py
File metadata and controls
135 lines (77 loc) · 2.42 KB
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
from des import (
generate_round_keys,
encrypt_block
)
# =========================
# STEP 1: READ IMAGE FILE
# =========================
file = open("input.jpg", "rb")
image_bytes = file.read()
file.close()
print("Image loaded successfully!")
# Store original image size
original_size = len(image_bytes)
print("Original File Size:", original_size, "bytes")
# =========================
# STEP 2: CONVERT IMAGE TO BINARY
# =========================
binary_data = ""
for byte in image_bytes:
binary_data += format(byte, '08b')
print("Binary conversion completed!")
# =========================
# STEP 3: SPLIT INTO 64-BIT BLOCKS
# =========================
blocks = []
for i in range(0, len(binary_data), 64):
block = binary_data[i:i+64]
# Padding if block smaller than 64 bits
if len(block) < 64:
block = block.ljust(64, '0')
blocks.append(block)
print("Total blocks:", len(blocks))
# =========================
# STEP 4: CREATE DES KEY
# =========================
key = "1010101010111011000010010001100000100111001101101100110011011101"
print("DES Key Loaded!")
# =========================
# STEP 5: GENERATE ROUND KEYS
# =========================
round_keys = generate_round_keys(key)
print("16 Round Keys Generated!")
# =========================
# STEP 6: ENCRYPT ALL BLOCKS
# =========================
encrypted_blocks = []
for block in blocks:
encrypted_block = encrypt_block(block, round_keys)
encrypted_blocks.append(encrypted_block)
print("All blocks encrypted successfully!")
# =========================
# STEP 7: COMBINE ENCRYPTED BLOCKS
# =========================
encrypted_data = ""
for block in encrypted_blocks:
encrypted_data += block
print("Encrypted data combined!")
# =========================
# STEP 8: CONVERT BINARY TO BYTES
# =========================
encrypted_bytes = bytearray()
for i in range(0, len(encrypted_data), 8):
byte = encrypted_data[i:i+8]
encrypted_bytes.append(int(byte, 2))
print("Binary converted to bytes!")
# =========================
# STEP 9: SAVE ENCRYPTED FILE
# =========================
encrypted_file = open("encrypted.des", "wb")
# Save original image size first (8 bytes)
encrypted_file.write(original_size.to_bytes(8, 'big'))
# Save encrypted data
encrypted_file.write(encrypted_bytes)
encrypted_file.close()
print("\nEncrypted file saved successfully!")
print("\nOutput File Generated:")
print("encrypted.des")