Last active
November 16, 2021 12:45
-
-
Save sachadee/dc3e0034869dc2fe97aaa5152dc86e57 to your computer and use it in GitHub Desktop.
AES CBC Encryption python with fix IV
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
import base64 | |
from Crypto.Cipher import AES | |
from Crypto.Util.Padding import pad,unpad | |
#CBC with Fix IV | |
data = 'I love Medium' | |
key = 'AAAAAAAAAAAAAAAA' #16 char for AES128 | |
#FIX IV | |
iv = 'BBBBBBBBBBBBBBBB'.encode('utf-8') #16 char for AES128 | |
def encrypt(data,key,iv): | |
data= pad(data.encode(),16) | |
cipher = AES.new(key.encode('utf-8'),AES.MODE_CBC,iv) | |
return base64.b64encode(cipher.encrypt(data)) | |
def decrypt(enc,key,iv): | |
enc = base64.b64decode(enc) | |
cipher = AES.new(key.encode('utf-8'), AES.MODE_CBC, iv) | |
return unpad(cipher.decrypt(enc),16) | |
encrypted = encrypt(data,key,iv) | |
print('encrypted CBC base64 : ',encrypted.decode("utf-8", "ignore")) | |
decrypted = decrypt(encrypted,key,iv) | |
print('data: ', decrypted.decode("utf-8", "ignore")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment