Created
May 22, 2022 06:04
-
-
Save Tarequzzaman/089ebb2b6b17411ca15a9c4c1cf181b9 to your computer and use it in GitHub Desktop.
AES 256 encryption and description in Python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# AES 256 encryption/decryption using pycrypto library | |
""" | |
used Python version: | |
-------------------- | |
Python3.8 | |
requirements: | |
------------ | |
pip3 install -U PyCryptodome | |
""" | |
import base64 | |
import hashlib | |
from Crypto.Cipher import AES | |
from Crypto import Random | |
BLOCK_SIZE = 16 | |
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE) | |
unpad = lambda s: s[:-ord(s[len(s) - 1:])] | |
key = "262731621gvhdwfd7237e623" #a constant key is maintain on both side encryption and decryption | |
def encrypt(raw, key): | |
private_key = hashlib.sha256(key.encode("utf-8")).digest() | |
raw = str.encode(pad(raw)) #convert str to byte | |
iv = Random.new().read(AES.block_size) | |
cipher = AES.new(private_key, AES.MODE_CBC, iv) | |
return base64.b64encode(iv + cipher.encrypt(raw)) | |
def decrypt(enc, key): | |
private_key = hashlib.sha256(key.encode("utf-8")).digest() | |
enc = base64.b64decode(enc) | |
iv = enc[:16] | |
cipher = AES.new(private_key, AES.MODE_CBC, iv) | |
return unpad(cipher.decrypt(enc[16:])) | |
#---------------------------------- | |
#.........Encryption............... | |
message = "This is a secret message" | |
encrypted = encrypt(message, key) | |
print(encrypted) | |
#---------------------------------- | |
#.........Decryption............... | |
decrypted = decrypt(encrypted, key) | |
print(bytes.decode(decrypted)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment