-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.py
25 lines (20 loc) · 842 Bytes
/
encrypt.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
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
def encrypt_message(message, public_key_path):
with open(public_key_path, 'rb') as pub_file:
public_key = RSA.import_key(pub_file.read())
cipher = PKCS1_OAEP.new(public_key)
encrypted_message = cipher.encrypt(message.encode())
# Overwrite and delete sensitive data
message = '\x00' * len(message)
del message, public_key, cipher
return encrypted_message
if __name__ == "__main__":
import sys
if len(sys.argv) != 3:
print("Usage: python encrypt.py <message> <public_key_path>")
else:
encrypted_message = encrypt_message(sys.argv[1], sys.argv[2])
with open('encrypted_message.bin', 'wb') as enc_file:
enc_file.write(encrypted_message)
print("Message encrypted and saved.")