-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05 Symmetric Encryption.py
130 lines (108 loc) · 4.55 KB
/
05 Symmetric 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Symmetric Encryption
# Symmetric encryption is a type of encryption in which same key is used for encryption as well as decryption
# We are using AES for Symmetric Encryption
# No Authentication
# Recommendation: Use AEAD for Authenticated Encryption
# Importing Libraries/Modules
from cryptography.hazmat.primitives.ciphers import Cipher
from cryptography.hazmat.primitives.ciphers import algorithms
from cryptography.hazmat.primitives.ciphers import modes
from cryptography.hazmat.primitives import padding
import os
# Class for Encryption
class Encryptor():
def __init__(self):
self.key = os.urandom(32)
def get_key(self):
return self.key
def encrypt_bytes(self, bytes_data):
iv = os.urandom(16)
cipher = Cipher(algorithms.AES(self.key), modes.CBC(iv))
encryptor = cipher.encryptor()
padder = padding.PKCS7(128).padder()
padded_data = padder.update(bytes_data)
padded_data += padder.finalize()
ciphertext = encryptor.update(padded_data)
ciphertext += encryptor.finalize()
fulltext = iv + ciphertext
return fulltext
def __file_chunks(self, file_object, chunk_size=65536):
while True:
data_chunk = file_object.read(chunk_size)
if not data_chunk:
break
yield data_chunk
def encrypt_file(self, plain_file_path, encrypted_file_path):
iv = os.urandom(16)
cipher = Cipher(algorithms.AES(self.key), modes.CBC(iv))
encryptor = cipher.encryptor()
padder = padding.PKCS7(128).padder()
with open(encrypted_file_path, "wb") as encrypted_file:
encrypted_file.write(iv)
with open(plain_file_path, "rb") as plain_file:
for data_chunk in self.__file_chunks(plain_file):
padded_data = padder.update(data_chunk)
ciphertext = encryptor.update(padded_data)
encrypted_file.write(ciphertext)
padded_data = padder.finalize()
ciphertext = encryptor.update(padded_data)
ciphertext += encryptor.finalize()
encrypted_file.write(ciphertext)
# Class for Decryption
class Decryptor():
def __init__(self, key):
self.key = key
def decrypt_bytes(self, fulltext):
iv = fulltext[:16]
ciphertext = fulltext[16:]
cipher = Cipher(algorithms.AES(self.key), modes.CBC(iv))
decryptor = cipher.decryptor()
plaintext = decryptor.update(ciphertext)
plaintext += decryptor.finalize()
unpadder = padding.PKCS7(128).unpadder()
unpadded_data = unpadder.update(plaintext)
unpadded_data += unpadder.finalize()
return unpadded_data
def __file_chunks(self, file_object, chunk_size=65536):
while True:
data_chunk = file_object.read(chunk_size)
if not data_chunk:
break
yield data_chunk
def decrypt_file(self, encrypted_file_path, decrypted_file_path):
with open(encrypted_file_path, "rb") as encrypted_file:
iv = encrypted_file.read(16)
cipher = Cipher(algorithms.AES(self.key), modes.CBC(iv))
decryptor = cipher.decryptor()
unpadder = padding.PKCS7(128).unpadder()
with open(decrypted_file_path, "wb") as decrypted_file:
for data_chunk in self.__file_chunks(encrypted_file):
plaintext = decryptor.update(data_chunk)
unpadded_data = unpadder.update(plaintext)
decrypted_file.write(unpadded_data)
plaintext = decryptor.finalize()
unpadded_data = unpadder.update(plaintext)
unpadded_data += unpadder.finalize()
decrypted_file.write(unpadded_data)
# Symmetric Encryption
print("Symmetric Encryption:")
# Creating Objects
encryptor = Encryptor()
key = encryptor.get_key()
decryptor = Decryptor(key)
# Encrypting Bytes
bytes_data = b"The quick brown fox jumps over the lazy dog"
fulltext = encryptor.encrypt_bytes(bytes_data)
# Decrypting Bytes
plaintext = decryptor.decrypt_bytes(fulltext)
print(plaintext)
# Creating a Sample File
plain_file_path = "Sample File.txt"
with open(plain_file_path, "w") as file:
file.write("The quick brown fox jumps over the lazy dog")
# Encrypting File
encrypted_file_path = "Sample File (Encrypted).txt"
encryptor.encrypt_file(plain_file_path, encrypted_file_path)
# Decrypting File
decrypted_file_path = "Sample File (Decrypted).txt"
decryptor.decrypt_file(encrypted_file_path, decrypted_file_path)