-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimple file Encryptor.py
More file actions
110 lines (90 loc) · 3.17 KB
/
Simple file Encryptor.py
File metadata and controls
110 lines (90 loc) · 3.17 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
import os
import sys
import time
import random
def save_key(key_value):
with open("xor_key.txt", "w") as key_file:
key_file.write(str(key_value))
print(f"[+] Encryption key saved as 'xor_key.txt'")
def load_key():
if not os.path.exists("xor_key.txt"):
print("[-] No key file found! Please generate one first.")
return None
try:
with open("xor_key.txt", "r") as key_file:
key_value = int(key_file.read().strip())
if 1 <= key_value <= 255:
return key_value
else:
print("[-] Invalid key found in file.")
return None
except Exception as e:
print(f"[-] Error reading key: {e}")
return None
def generate_key():
key_value = random.randint(1, 255)
save_key(key_value)
print(f"[+] New key generated successfully! (Key: {key_value})")
def process_file(filename, key_value, mode="encrypt"):
if not os.path.exists(filename):
print("[-] File not found. Please check the file name.")
return
if mode == "encrypt":
output_file = filename + ".enc"
else:
output_file = filename[:-4] + "_decrypted.txt" if filename.endswith(".enc") else filename + "_decrypted.txt"
try:
with open(filename, "rb") as infile:
data = infile.read()
processed_data = bytes([byte ^ key_value for byte in data])
with open(output_file, "wb") as outfile:
outfile.write(processed_data)
print(f"[+] File '{filename}' {mode}ed successfully → '{output_file}'")
except Exception as e:
print(f"[-] Error while processing file: {e}")
def show_progress(message):
print(f"{message}", end="", flush=True)
for _ in range(3):
print(".", end="", flush=True)
time.sleep(0.4)
print(" Done!")
def show_menu():
print("\n" + "=" * 55)
print(" SIMPLE XOR FILE ENCRYPTOR")
print("=" * 55)
print("1. Generate New Key")
print("2. Encrypt a File")
print("3. Decrypt a File")
print("4. Exit")
print("=" * 55)
def main():
while True:
show_menu()
choice = input("Enter your choice (1-4): ").strip()
if choice == "1":
generate_key()
elif choice == "2":
key_value = load_key()
if not key_value:
continue
filename = input("Enter the file name to encrypt: ").strip()
show_progress("Encrypting")
process_file(filename, key_value, "encrypt")
elif choice == "3":
key_value = load_key()
if not key_value:
continue
filename = input("Enter the file name to decrypt: ").strip()
show_progress("Decrypting")
process_file(filename, key_value, "decrypt")
elif choice == "4":
print("\nExiting program", end="")
for _ in range(3):
print(".", end="", flush=True)
time.sleep(0.4)
print("\nGoodbye, have a great day!")
sys.exit()
else:
print("[-] Invalid choice! Please enter a number between 1 and 4.")
if __name__ == "__main__":
main()