Skip to content

Instantly share code, notes, and snippets.

View DavidTorresOcana's full-sized avatar
😀
“The Man who says he can, and the man who says he can not.. Are both correct”

David Torres Ocaña DavidTorresOcana

😀
“The Man who says he can, and the man who says he can not.. Are both correct”
View GitHub Profile
@ahbanavi
ahbanavi / encryption.php
Last active May 1, 2024 23:18
Encrypt / Decrypt JSON data between Python and PHP using AES 256 GCM
<?php
const PASSPHRASE = ''; // use 'openssl rand -hex 32' to generate key, same with python
function encrypt(array $data): string
{
$data_json_64 = base64_encode(json_encode($data));
$secret_key = hex2bin(PASSPHRASE);
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-gcm'));
$tag = '';
$encrypted_64 = openssl_encrypt($data_json_64, 'aes-256-gcm', $secret_key, 0, $iv, $tag);