forked from Py-Contributors/awesomeScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Encryption.py
74 lines (63 loc) · 2.24 KB
/
Encryption.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
from Crypto.Cipher import AES
from cryptography.fernet import Fernet
import Crypto.Random as Random
class ImageEncryption:
"""
this class for image encryption
"""
__image_key = Random.new().read(AES.block_size)
__init_vector = Random.new().read(AES.block_size)
def encrypt_image(self, par_image, par_enc_image):
"""
this function for encrypting image
args:
par_image: the image that is needs to be encrypted
par_enc_image: an image after encryption
return:none
"""
with open(par_image, 'rb') as input_file:
input_data = input_file.read()
cfb_cipher = AES.new(self.__image_key, AES.MODE_CFB,
self.__init_vector)
enc_data = cfb_cipher.encrypt(bytearray(input_data))
with open(par_enc_image, "wb") as enc_file:
enc_file.write(enc_data)
def decrypt_image(self, par_enc_image, par_real_image):
"""
this function for decrypting image
args:
par_enc_image: encrypted image
par_enc_image: the real image
return:none
"""
with open(par_enc_image, "rb") as enc_file:
enc_data = enc_file.read()
cfb_decipher = AES.new(self.__image_key, AES.MODE_CFB,
self.__init_vector)
plain_data = cfb_decipher.decrypt(enc_data)
with open("output.jpg", "wb") as output_file:
output_file.write(plain_data)
class TextEncryption:
"""
this class for text encryption
install cryptography.fernet
"""
__txt_key = Fernet.generate_key()
def encrypt_text(self, par_txt):
"""
this function for encrypting text
args:
par_txt: text that needs to be encrypted
return:encrypted text as string
"""
cipher_suite = Fernet(self.__txt_key)
return cipher_suite.encrypt(bytes(par_txt, "utf-8"))
def decrypt_text(self, par_enc_txt):
"""
this function for decrypting text
args:
par_enc_txt: encrypted text
return:real text
"""
cipher_suite = Fernet(self.__txt_key)
return str(cipher_suite.decrypt(par_enc_txt))