aes python code code example
Example 1: python AES
from Crypto.Cipher import AES
import binascii,os
import random, string
iv = os.urandom(16)
aes_mode = AES.MODE_CBC
key = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(16))
print(key)
encryptor = AES.new(key, aes_mode, iv)
def aes_encrypt(plaintext):
plaintext = convert_to_16(plaintext)
ciphertext = encryptor.encrypt(plaintext)
return ciphertext
def convert_to_16(plaintext):
add = 16 - (len(plaintext) % 16)
return(plaintext + ' ' * add)
Encrypted = aes_encrypt('Jaisal ')
print("Encrypted message :",Encrypted)
Example 2: aes in python
>>> from Crypto.Cipher import AES
>>>
>>> key = b'Sixteen byte key'
>>> cipher = AES.new(key, AES.MODE_EAX, nonce=nonce)
>>> plaintext = cipher.decrypt(ciphertext)
>>> try:
>>> cipher.verify(tag)
>>> print("The message is authentic:", plaintext)
>>> except ValueError:
>>> print("Key incorrect or message corrupted")