Last active
June 19, 2020 14:50
-
-
Save nenriquez/a054b1c9e860289026782291129492d3 to your computer and use it in GitHub Desktop.
PYTHON / PHP compatible AES encryption.
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 | |
/** | |
* Encrypt data using AES Cipher (CBC) with 128 bit key | |
* | |
* @param type $key - key to use should be 16 bytes long (128 bits) | |
* @param type $iv - initialization vector | |
* @param type $data - data to encrypt | |
* @return encrypted data in base64 encoding with iv attached at end after a : | |
*/ | |
function encrypt($key, $iv, $data) | |
{ | |
$padding = ceil(strlen($data) / 16) * 16; | |
$data = str_pad("$data", $padding, "\x00"); | |
return base64_encode(openssl_encrypt($data, 'AES-128-CBC', $key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv)); | |
} | |
/** | |
* Decrypt data using AES Cipher (CBC) with 128 bit key | |
* | |
* @param type $key - key to use should be 16 bytes long (128 bits) | |
* @param type $data - data to be decrypted in base64 encoding with iv attached at the end after a : | |
* @return decrypted data | |
*/ | |
function decrypt($key, $iv, $data) | |
{ | |
$decrypt = openssl_decrypt(base64_decode($data), 'AES-128-CBC', $key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv); | |
return str_replace("\x00", "", $decrypt); | |
} | |
$iv = 'fedcba9876543210'; | |
$key = '12345678abcdefgh'; | |
$data = "facebook+1523"; | |
print "To encrypt < 16: $data\n"; | |
$crypted = encrypt($key, $iv, $data); | |
print "Crypted: $crypted\n"; | |
$decrypted = decrypt($key, $iv, $crypted); | |
print "Decrypted: $decrypted\n\n"; | |
$data = "facebook+1523543"; | |
print "To encrypt == 16: $data\n"; | |
$crypted = encrypt($key, $iv, $data); | |
print "Crypted: $crypted\n"; | |
$decrypted = decrypt($key, $iv, $crypted); | |
print "Decrypted: $decrypted\n\n"; | |
$data = "facebook+152354332"; | |
print "To encrypt > 16: $data\n"; | |
$crypted = encrypt($key, $iv, $data); | |
print "Crypted: $crypted\n"; | |
$decrypted = decrypt($key, $iv, $crypted); | |
print "Decrypted: $decrypted\n\n"; | |
/* | |
RESULT: | |
To encrypt < 16: facebook+1523 | |
Crypted: oDAXGlc7cQyngARlWmshdA== | |
Decrypted: facebook+1523 | |
To encrypt == 16: facebook+1523543 | |
Crypted: BGLZf3mdETNSgVqeSQXwNA== | |
Decrypted: facebook+1523543 | |
To encrypt > 16: facebook+152354332 | |
Crypted: BGLZf3mdETNSgVqeSQXwNA0XXXXfc4bjucOQqUyvaJ4= | |
Decrypted: facebook+152354332 | |
*/ |
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
import math | |
import base64 | |
from Crypto.Cipher import AES | |
def encrypt(key, iv, data): | |
padding_size = int(math.ceil(len(data) / 16.0)) * 16 | |
data = data.ljust(padding_size, '\x00') | |
cipher = AES.new(key, AES.MODE_CBC, iv) | |
return base64.b64encode(cipher.encrypt(data)).decode('ascii') | |
def decrypt(key, iv, data): | |
cipher = AES.new(key, AES.MODE_CBC, iv) | |
return cipher.decrypt(base64.b64decode(data)).replace('\x00', '') | |
if __name__ == '__main__': | |
iv = 'fedcba9876543210' | |
key = '12345678abcdefgh' | |
data = "facebook+1523" | |
print("To encrypt < 16: {}".format(data)) | |
crypted = encrypt(key, iv, data) | |
print("Encriptado: {}".format(crypted)) | |
decrypted = decrypt(key, iv, crypted) | |
print("Decrypted: {}\n".format(decrypted)) | |
data = "facebook+1523543" | |
print("To encrypt == 16: {}".format(data)) | |
crypted = encrypt(key, iv, data) | |
print("Crypted: {}".format(crypted)) | |
decrypted = decrypt(key, iv, crypted) | |
print("Decrypted: {}\n".format(decrypted)) | |
data = "facebook+152354332" | |
print("To encrypt > 16: {}".format(data)) | |
crypted = encrypt(key, iv, data) | |
print("Crypted: {}".format(crypted)) | |
decrypted = decrypt(key, iv, crypted) | |
print("Decrypted: {}\n".format(decrypted)) | |
/* | |
RESULT: | |
To encrypt < 16: facebook+1523 | |
Crypted: oDAXGlc7cQyngARlWmshdA== | |
Decrypted: facebook+1523 | |
To encrypt == 16: facebook+1523543 | |
Crypted: BGLZf3mdETNSgVqeSQXwNA== | |
Decrypted: facebook+1523543 | |
To encrypt > 16: facebook+152354332 | |
Crypted: BGLZf3mdETNSgVqeSQXwNA0XXXXfc4bjucOQqUyvaJ4= | |
Decrypted: facebook+152354332 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment