Created
February 27, 2020 05:10
-
-
Save x011/5565d3fe2dcece3fe91db61edd1cc7f3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
''' | |
Project: curinga | |
Time: 27/02/2020 04:45 | |
''' | |
import base64 | |
import hashlib | |
from Crypto import Random | |
from Crypto.Cipher import AES | |
block_size = 16 | |
pad = lambda s: s + (block_size - len(s) % block_size) * chr(block_size - len(s) % block_size) | |
unpad = lambda s: s[0:-ord(s[-1:])] | |
iv = Random.new().read(AES.block_size) # Random IV | |
def get_private_key(secret_key, salt): | |
return hashlib.pbkdf2_hmac('SHA256', secret_key.encode(), salt.encode(), 65536, 32) | |
def encrypt_with_AES(message, secret_key, salt): | |
private_key = get_private_key(secret_key, salt) | |
message = pad(message).encode() | |
cipher = AES.new(private_key, AES.MODE_CBC, iv) | |
cipher_bytes = base64.b64encode(iv + cipher.encrypt(message)) | |
return bytes.decode(cipher_bytes) | |
def decrypt_with_AES(encoded, secret_key, salt): | |
private_key = get_private_key(secret_key, salt) | |
cipher_text = base64.b64decode(encoded) | |
iv = cipher_text[:AES.block_size] | |
cipher = AES.new(private_key, AES.MODE_CBC, iv) | |
plain_bytes = unpad(cipher.decrypt(cipher_text[block_size:])); | |
return bytes.decode(plain_bytes) | |
secret_key = "123" | |
salt = "anySaltYouCanUseOfOn" | |
plain_text = "9999" | |
cipher = encrypt_with_AES(plain_text, secret_key, salt) | |
print("Cipher: " + cipher) | |
#cipher = "xrNbcYkfIlbXIeU6LJscBkz8DV2ivOq80Lxk2zj3LX4=" | |
decrypted = decrypt_with_AES(cipher, secret_key, salt) | |
print("Decrypted " + decrypted) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment