Skip to content

Instantly share code, notes, and snippets.

@pingpoli
Created July 5, 2020 15:22
Show Gist options
  • Save pingpoli/a91dc4e7ca45e813c9fde50ab2f5cef3 to your computer and use it in GitHub Desktop.
Save pingpoli/a91dc4e7ca45e813c9fde50ab2f5cef3 to your computer and use it in GitHub Desktop.
<?php
function ppnEncrypt( $data , $key )
{
$method = 'aes-256-gcm';
$key = base64_decode( $key );
$iv = openssl_random_pseudo_bytes( openssl_cipher_iv_length( $method ) );
$tag = ""; // openssl_encrypt will fill this
$result = openssl_encrypt( $data , $method , $key , OPENSSL_RAW_DATA , $iv , $tag , "" , 16 );
return base64_encode( $iv.$tag.$result );
}
function ppnDecrypt( $data , $key )
{
$method = 'aes-256-gcm';
$data = base64_decode( $data );
$key = base64_decode( $key );
$ivLength = openssl_cipher_iv_length( $method );
$iv = substr( $data , 0 , $ivLength );
$tag = substr( $data , $ivLength , 16 );
$text = substr( $data , $ivLength+16 );
return openssl_decrypt( $text , $method , $key , OPENSSL_RAW_DATA , $iv , $tag );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment