forked from kleo/pico-2fa-totp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cryptor.py
31 lines (27 loc) · 912 Bytes
/
cryptor.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
26
27
28
29
import hashlib
import cryptolib
def encrypt(filename,key):
hasher=hashlib.sha256(key)
digest=hasher.digest()
encryptor = cryptolib.aes(digest, 1)
try:
with open(filename,"r") as f:
data=f.read()
data_bytes = data.encode()
encdata=encryptor.encrypt(data_bytes + b'\x00' * ((16 - (len(data_bytes) % 16)) % 16))
with open("{}.encoded".format(filename),"w") as fw:
return(fw.write(encdata))
except Exception as e:
print("Error: cryptor::encrypt:",e)
return None
def decrypt(filename,key):
hasher=hashlib.sha256(key)
digest=hasher.digest()
try:
with open(filename,"rb") as f:
dataread=f.read()
decryptor = cryptolib.aes(digest, 1)
return decryptor.decrypt(dataread)
except Exception as e:
print("Error: cryptor::decrypt:",e)
return None