-
Notifications
You must be signed in to change notification settings - Fork 2
/
crypter.py
86 lines (76 loc) · 3.01 KB
/
crypter.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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding:utf-8 -*-
import os
from base64 import b64encode, b64decode
from cryptography.fernet import Fernet, MultiFernet
ext = ".four"
BLOCK_SIZE = 32
FCRYPTERAES = True
if FCRYPTERAES:
from Crypto.Cipher import AES
from Crypto import Random
def pad(s):
return s + b"\0" * (BLOCK_SIZE - len(s) % BLOCK_SIZE)
class Fcrypter:
def __init__(self, key):
self.key = pad(key.encode())
self.path = os.getcwd()
self.extend = os.sep
self.merge = self.path+self.extend
def aesCrypt(self, veri):
iv = Random.new().read(AES.block_size)
cipher = AES.new(self.key, AES.MODE_CBC, iv)
cipher_bytes = cipher.encrypt(pad(veri))
data = iv + cipher_bytes
return b64encode(data)
def aesDecrypt(self, crypted_data):
crypted_data = b64decode(crypted_data)
iv = crypted_data[:AES.block_size]
cipher = AES.new(self.key, AES.MODE_CBC, iv)
decrypted = cipher.decrypt(crypted_data[AES.block_size:]).rstrip(b"\0")
return decrypted
def encryptFile(self, file, aes=FCRYPTERAES):
if not self.extend in file:
file = self.merge+file
if aes:
with open(file, "rb") as _file:
text = _file.read()
encrypted = self.aesCrypt(text)
with open(file + ext, "wb") as encrypted_file:
encrypted_file.write(encrypted)
os.remove(file)
else:
fernet_cipher = MultiFernet([Fernet(b64encode(self.key)), Fernet(b64encode(self.key[::-1]))])
with open(file, "rb") as _file:
data = _file.read()
crypted = fernet_cipher.encrypt(data)
with open(file + ext, "wb") as new:
new.write(crypted)
os.remove(file)
def decryptFile(self, file, aes=FCRYPTERAES):
if not self.extend in file:
file = self.merge + file
if aes:
with open(file, "rb") as _file:
crypted_text = _file.read()
decrypted = self.aesDecrypt(crypted_text)
with open(file[:-int(len(ext))], "wb") as decrypted_file:
decrypted_file.write(decrypted)
os.remove(file)
else:
fernet_cipher = MultiFernet([Fernet(b64encode(self.key)), Fernet(b64encode(self.key[::-1]))])
with open(file, "rb") as _file:
data = _file.read()
decrypted = fernet_cipher.decrypt(data)
with open(str(file).rstrip(ext), "wb") as new:
new.write(decrypted)
os.remove(file)
def encryptFolder(self, path, aes=FCRYPTERAES):
for root, dirs, files in os.walk(path):
for x in files:
file = os.path.join(root, x)
self.encryptFile(file=file, aes=aes)
def decryptFolder(self, path, aes=FCRYPTERAES):
for root, dirs, files in os.walk(path):
for x in files:
file = os.path.join(root, x)
self.decryptFile(file=file, aes=aes)