-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
67 lines (60 loc) · 2.43 KB
/
python.py
File metadata and controls
67 lines (60 loc) · 2.43 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
import datetime
from Crypto.PublicKey import RSA
from Crypto.Cipher import AES, PKCS1_OAEP
from Crypto.Util import Padding
import sys
class AESEncryption:
def __init__(self) -> None:
self.operation=sys.argv[1]
self.file1 = sys.argv[2]
self.file2 = sys.argv[3]
self.aes_key = b'Sixteen byte key'
self.cipher_aes =cipher_aes = AES.new(key=self.aes_key, mode=AES.MODE_CBC, iv=b'\x00'*16)
if(self.operation == "-e"):
try:
self.createKeys()
self.encrypt()
except Exception as e:
print(e)
elif(self.operation == "-d"):
try:
self.createKeys()
self.decrypt()
except Exception as e:
print(e)
else:
print("Invalid operation trigger \n try -e for encrypting \n try -d for decrypting")
def createKeys(self):
key = RSA.generate(2048)
public_key = key.publickey().export_key("PEM")
private_key = key.export_key("PEM")
with open("publicKey.pem", "wb") as f:
f.write(public_key)
with open("privateKey.pem", "wb") as f:
f.write(private_key)
def encrypt(self):
# encrypt the data with AES
with open(self.file2,"rb") as file:
data =file.read()
padded_data = Padding.pad(data, AES.block_size)
encrypted_data = self.cipher_aes.encrypt(padded_data)
# encrypt the AES key with RSA
public_key = RSA.import_key(open("publicKey.pem").read())
cipher_rsa = PKCS1_OAEP.new(public_key)
encrypted_aes_key = cipher_rsa.encrypt(self.aes_key)
with open(self.file1,"wb") as file:
file.write(encrypted_data)
def decrypt(self):
# decrypt the AES key with RSA
private_key = RSA.import_key(open("privateKey.pem").read())
cipher_rsa = PKCS1_OAEP.new(private_key)
encrypted_aes_key = cipher_rsa.encrypt(self.aes_key)
decrypted_aes_key = cipher_rsa.decrypt(encrypted_aes_key)
# decrypt the data with AES
cipher_aes = AES.new(key=decrypted_aes_key, mode=AES.MODE_CBC, iv=b'\x00'*16)
with open(self.file1,"rb") as file:
data = file.read()
decrypted_data = Padding.unpad(cipher_aes.decrypt(data), AES.block_size)
with open(self.file2,"w") as file:
file.write(decrypted_data.decode())
AESEncryption()