-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt_file.py
More file actions
41 lines (30 loc) · 1.15 KB
/
encrypt_file.py
File metadata and controls
41 lines (30 loc) · 1.15 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
import hashlib
from Crypto import Random
from Crypto.Cipher import AES
from base64 import b64encode, b64decode
import time, rsa
class edFile:
def __init__(self, key):
#convert our key to 256 bit hash
self.key = hashlib.sha256(key.encode()).digest()
#blocksize of AES i.e 128 bit
self.block_s = AES.block_size
#pad the file content to fit the block_size
def padIt(self, file):
content = open(file, "rb").read()
tChars = self.block_s - (len(content) % self.block_s)
return content + bytes(chr(tChars) * tChars, "utf-8")
def unpadIt(self, text):
return text[:text[-1] * -1]
def encrypt(self, file):
padded = self.padIt(file)
#initialize a vector of size as the block_size
starting_vector = Random.new().read(self.block_s)
cipher = AES.new(self.key, AES.MODE_CBC, starting_vector)
return b64encode(starting_vector + cipher.encrypt(padded)).decode("utf-8")
def decrypt(self, encryption, privKey):
key = hashlib.sha256(rsa.decrypt(self.key, privKey)).digest()
decoded = b64decode(encryption)
vector = decoded[:self.block_s]
cipher = AES.new(key, AES.MODE_CBC, vector)
return self.unpadIt(cipher.decrypt(decoded[self.block_s:]))