Module licenseware.utils.aes
Expand source code
import hashlib
from Crypto import Random
from Crypto.Cipher import AES
from base64 import b64encode, b64decode
class AESCipher:
"""
Usage:
```py
password = 'secret'
text_to_encrypt = 'hello john'
aes = AESCipher(password)
encrypted_text = aes.encrypt(text_to_encrypt)
decrypted_text = aes.decrypt(encrypted_text)
```
"""
def __init__(self, key):
self.block_size = AES.block_size
self.key = hashlib.sha256(key.encode()).digest()
def encrypt(self, plain_text):
plain_text = self.__pad(plain_text)
iv = Random.new().read(self.block_size)
cipher = AES.new(self.key, AES.MODE_CBC, iv)
encrypted_text = cipher.encrypt(plain_text.encode())
iv_encrypted_text = iv + encrypted_text
return b64encode(iv_encrypted_text, altchars=b'-:').decode("utf-8")
def decrypt(self, encrypted_text):
encrypted_text = b64decode(encrypted_text, altchars=b'-:')
iv = encrypted_text[:self.block_size]
cipher = AES.new(self.key, AES.MODE_CBC, iv)
plain_text = cipher.decrypt(encrypted_text[self.block_size:]).decode("utf-8")
return self.__unpad(plain_text)
def __pad(self, plain_text):
number_of_bytes_to_pad = self.block_size - len(plain_text) % self.block_size
ascii_string = chr(number_of_bytes_to_pad)
padding_str = number_of_bytes_to_pad * ascii_string
padded_plain_text = plain_text + padding_str
return padded_plain_text
@staticmethod
def __unpad(plain_text):
last_character = plain_text[len(plain_text) - 1:]
return plain_text[:-ord(last_character)]
Classes
class AESCipher (key)
-
Usage:
password = 'secret' text_to_encrypt = 'hello john' aes = AESCipher(password) encrypted_text = aes.encrypt(text_to_encrypt) decrypted_text = aes.decrypt(encrypted_text)
Expand source code
class AESCipher: """ Usage: ```py password = 'secret' text_to_encrypt = 'hello john' aes = AESCipher(password) encrypted_text = aes.encrypt(text_to_encrypt) decrypted_text = aes.decrypt(encrypted_text) ``` """ def __init__(self, key): self.block_size = AES.block_size self.key = hashlib.sha256(key.encode()).digest() def encrypt(self, plain_text): plain_text = self.__pad(plain_text) iv = Random.new().read(self.block_size) cipher = AES.new(self.key, AES.MODE_CBC, iv) encrypted_text = cipher.encrypt(plain_text.encode()) iv_encrypted_text = iv + encrypted_text return b64encode(iv_encrypted_text, altchars=b'-:').decode("utf-8") def decrypt(self, encrypted_text): encrypted_text = b64decode(encrypted_text, altchars=b'-:') iv = encrypted_text[:self.block_size] cipher = AES.new(self.key, AES.MODE_CBC, iv) plain_text = cipher.decrypt(encrypted_text[self.block_size:]).decode("utf-8") return self.__unpad(plain_text) def __pad(self, plain_text): number_of_bytes_to_pad = self.block_size - len(plain_text) % self.block_size ascii_string = chr(number_of_bytes_to_pad) padding_str = number_of_bytes_to_pad * ascii_string padded_plain_text = plain_text + padding_str return padded_plain_text @staticmethod def __unpad(plain_text): last_character = plain_text[len(plain_text) - 1:] return plain_text[:-ord(last_character)]
Methods
def decrypt(self, encrypted_text)
-
Expand source code
def decrypt(self, encrypted_text): encrypted_text = b64decode(encrypted_text, altchars=b'-:') iv = encrypted_text[:self.block_size] cipher = AES.new(self.key, AES.MODE_CBC, iv) plain_text = cipher.decrypt(encrypted_text[self.block_size:]).decode("utf-8") return self.__unpad(plain_text)
def encrypt(self, plain_text)
-
Expand source code
def encrypt(self, plain_text): plain_text = self.__pad(plain_text) iv = Random.new().read(self.block_size) cipher = AES.new(self.key, AES.MODE_CBC, iv) encrypted_text = cipher.encrypt(plain_text.encode()) iv_encrypted_text = iv + encrypted_text return b64encode(iv_encrypted_text, altchars=b'-:').decode("utf-8")