-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_file.py
More file actions
119 lines (91 loc) · 4.06 KB
/
main_file.py
File metadata and controls
119 lines (91 loc) · 4.06 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
from rcncipher import encrypt, decrypt
from datetime import datetime
def text_to_128_bit_hex_chunks(text):
# Convert text to bytes
text_bytes = text.encode('utf-8')
# Calculate the number of 128-bit chunks
num_chunks = (len(text_bytes) + 15) // 16
# Pad the text_bytes to make its length a multiple of 16 bytes
padded_bytes = text_bytes.ljust(num_chunks * 16, b'\0')
# Divide the padded bytes into 128-bit chunks
chunks = [padded_bytes[i:i+16] for i in range(0, len(padded_bytes), 16)]
# Convert each chunk to hexadecimal representation
hex_chunks = [chunk.hex() for chunk in chunks]
# print(hex_chunks)
return hex_chunks
def text_to_hex(text):
# Convert text to bytes
text_bytes = text.encode('utf-8')
# Convert bytes to hexadecimal representation
hex_representation = text_bytes.hex()
return hex_representation
def hex_to_text(hex_string):
# Convert hexadecimal string to bytes
byte_string = bytes.fromhex(hex_string)
# Decode bytes to text using UTF-8 encoding
text = byte_string.decode('utf-8')
return text
def read_file(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
return content
except FileNotFoundError:
print(f"File '{file_path}' not found.")
return None
except Exception as e:
print(f"Error reading file '{file_path}': {e}")
return None
def write_to_file(file_path, content):
try:
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
print(f"Content successfully written to '{file_path}'.")
except Exception as e:
print(f"Error writing to file '{file_path}': {e}")
# User input to perform encryption or decryption
while(True):
choice = input("Enter 'e' for encryption or 'd' for decryption: ")
if choice == 'e':
# Get the key and plaintext from the user
key = text_to_hex(input("Enter the key: "))
path = input("Enter the file name to encrypt (text file): ")
plaintext = read_file(path)
# Convert the plaintext into 128-bit chunks
chunks = text_to_128_bit_hex_chunks(plaintext)
# Encrypt each 128-bit chunk
ciphertext = ''.join(encrypt(chunk, key) for chunk in chunks)
#write to file
#save file to encrypted-time-date.txt
#include date time string in path
current_datetime = datetime.now()
# Format the date and time as a string
datetime_string = current_datetime.strftime("%Y-%m-%d-%H-%M-%S")
write_to_file(f"encrypted--{datetime_string}-{path}", ciphertext)
# print("Ciphertext:", ciphertext)
# break
elif choice == 'd':
# Get the key and ciphertext from the user
key = text_to_hex(input("Enter the key: "))
if len(key) <= 32: # Each hexadecimal character represents 4 bits
print("Hexadecimal representation:", key)
else:
raise ValueError("Hexadecimal representation exceeds 128 bits.")
path = input("Enter the file name to decrypt (text file)")
ciphertext = read_file(path)
# Decrypt the ciphertext
decrypted_chunks = [hex_to_text(decrypt(ciphertext[i:i+32], key)) for i in range(0, len(ciphertext), 32)]
# Remove the padding and convert the decrypted chunks to text
decrypted_text = ''.join(decrypted_chunks)
# print("Decrypted text:", decrypted_text)
#save file to decrypted-time-date.txt
#include date time string in path
current_datetime = datetime.now()
# Format the date and time as a string
datetime_string = current_datetime.strftime("%Y-%m-%d-%H-%M-%S")
write_to_file(f"decrypted--{datetime_string}-{path}", decrypted_text)
elif choice == 'exit':
break
else:
choice = input("Invalid choice. Type exit to, well, exit. Enter 'e' for encryption or 'd' for decryption: ")
continue