-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextEncryption.py
More file actions
32 lines (23 loc) · 961 Bytes
/
textEncryption.py
File metadata and controls
32 lines (23 loc) · 961 Bytes
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
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
import os
BLOCK_SIZE = 16
def pad(text):
padding_size = BLOCK_SIZE - len(text) % BLOCK_SIZE
padding = bytes([padding_size] * padding_size)
return text + padding
def unpad(text):
padding_size = text[-1]
return text[:-padding_size]
def encrypt(plaintext, key):
iv = os.urandom(BLOCK_SIZE)
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()
ciphertext = encryptor.update(pad(plaintext)) + encryptor.finalize()
return iv + ciphertext
def decrypt(inputText, key):
iv = inputText[:BLOCK_SIZE]
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
decryptor = cipher.decryptor()
outputText = unpad(decryptor.update(inputText[BLOCK_SIZE:]) + decryptor.finalize())
return outputText