I've implemented a wrapper for AES 256 CTR mode using the cryptography.hazmat module, I am wondering if there are any vulnerabilities in my implementation, specifically about the counter and its encoding. Here is the code:
from cryptography.hazmat.primitives.ciphers import Cipher from cryptography.hazmat.primitives.ciphers.algorithms import AES from cryptography.hazmat.primitives.ciphers.modes import CTR from cryptography.hazmat.backends import default_backend as backend from base58 import b58encode,b58decode import os #AES Cipher Class class AES_Cipher: #Initialise Class, Set Countner And Key def __init__(self,key): self.counter = 0 self.key = key #AES 256 Requirement assert len(self.key) == 32 #Encryption Function def encrypt(self,plain_text): plain_text = plain_text.encode() self.counter += 1 cipher = Cipher(AES(self.key),CTR(self.padCounter()),backend()) encryption_engine = cipher.encryptor() cipher_text = self.padCounter() + encryption_engine.update(plain_text) + encryption_engine.finalize() return b58encode(cipher_text) #Decryption Function def decrypt(self,cipher_text): cipher_text = b58decode(cipher_text) self.counter = cipher_text[:16] cipher = Cipher(AES(self.key),CTR(self.counter),backend()) decryption_engine = cipher.decryptor() plain_text = decryption_engine.update(cipher_text[16:]) + decryption_engine.finalize() return plain_text.decode() #Pad The Counter Into 16 Bytes def padCounter(self): return bytes(str(self.counter).zfill(16),"ascii")
Usage:
key = os.urandom(32) aes_engine = AES_Cipher(key) aes_engine.encrypt("hello world") aes_engine.decrypt(b"7WkHvZEJRr8yMEasvh3TESoW8nBTkEUNVu2Li")