-
-
Save MooneDrJune/fca8daaf64782621f8e74ac48e27017a to your computer and use it in GitHub Desktop.
Encrypt - Decrypt AES from/to Python PyCryptodome from/to PHP openssl
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
<?php | |
// use to generate key : 'openssl rand -hex 32' | |
function my_encrypt($data, $passphrase) { | |
$secret_key = hex2bin($passphrase); | |
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc')); | |
$encrypted_64 = openssl_encrypt($data, 'aes-256-cbc', $secret_key, 0, $iv); | |
$iv_64 = base64_encode($iv); | |
$json = new stdClass(); | |
$json->iv = $iv_64; | |
$json->data = $encrypted_64; | |
return base64_encode(json_encode($json)); | |
} | |
function my_decrypt($data, $passphrase) { | |
$secret_key = hex2bin($passphrase); | |
$json = json_decode(base64_decode($data)); | |
$iv = base64_decode($json->{'iv'}); | |
$encrypted_64 = $json->{'data'}; | |
$data_encrypted = base64_decode($encrypted_64); | |
$decrypted = openssl_decrypt($data_encrypted, 'aes-256-cbc', $secret_key, OPENSSL_RAW_DATA, $iv); | |
return $decrypted; | |
} |
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
#!/usr/bin/env python3 | |
# pip install pycryptodome | |
import json | |
import binascii | |
from Crypto.Cipher import AES | |
from Crypto import Random | |
import base64 | |
def my_encrypt(data, passphrase): | |
""" | |
Encrypt using AES-256-CBC with random/shared iv | |
'passphrase' must be in hex, generate with 'openssl rand -hex 32' | |
""" | |
try: | |
key = binascii.unhexlify(passphrase) | |
pad = lambda s : s+chr(16-len(s)%16)*(16-len(s)%16) | |
iv = Random.get_random_bytes(16) | |
cipher = AES.new(key, AES.MODE_CBC, iv) | |
encrypted_64 = base64.b64encode(cipher.encrypt(pad(data).encode())).decode('ascii') | |
iv_64 = base64.b64encode(iv).decode('ascii') | |
json_data = {} | |
json_data['iv'] = iv_64 | |
json_data['data'] = encrypted_64 | |
clean = base64.b64encode(json.dumps(json_data).encode('ascii')) | |
except Exception as e: | |
print("Cannot encrypt datas...") | |
print(e) | |
exit(1) | |
return clean | |
def my_decrypt(data, passphrase): | |
""" | |
Decrypt using AES-256-CBC with iv | |
'passphrase' must be in hex, generate with 'openssl rand -hex 32' | |
# https://stackoverflow.com/a/54166852/11061370 | |
""" | |
try: | |
unpad = lambda s : s[:-s[-1]] | |
key = binascii.unhexlify(passphrase) | |
encrypted = json.loads(base64.b64decode(data).decode('ascii')) | |
encrypted_data = base64.b64decode(encrypted['data']) | |
iv = base64.b64decode(encrypted['iv']) | |
cipher = AES.new(key, AES.MODE_CBC, iv) | |
decrypted = cipher.decrypt(encrypted_data) | |
clean = unpad(decrypted).decode('ascii').rstrip() | |
except Exception as e: | |
print("Cannot decrypt datas...") | |
print(e) | |
exit(1) | |
return clean |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment