-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
44 lines (28 loc) · 1.11 KB
/
main.py
File metadata and controls
44 lines (28 loc) · 1.11 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
import rsa
(public_key, private_key) = rsa.newkeys(2048)
with open("public_key.pem", "wb") as f:
f.write(public_key.save_pkcs1())
with open("private_key.pem", "wb") as f:
f.write(private_key.save_pkcs1())
with open("public_key.pem", "rb") as f:
public_key = rsa.PublicKey.load_pkcs1(f.read())
file_path = input("Enter the path to the text file: ")
try:
with open(file_path, "rb") as f:
data = f.read()
encrypted_data = rsa.encrypt(data, public_key)
with open("encrypted_data.txt", "wb") as f:
f.write(encrypted_data)
print("Data encrypted successfully!")
private_key_path = input("Enter the path to the private key file: ")
try:
with open(private_key_path, "rb") as f:
private_key = rsa.PrivateKey.load_pkcs1(f.read())
decrypted_data = rsa.decrypt(encrypted_data, private_key)
with open("decrypted_data.txt", "wb") as f:
f.write(decrypted_data)
print("Data decrypted successfully!")
except FileNotFoundError:
print("Private key file not found!")
except FileNotFoundError:
print("File not found!")