Created
July 5, 2020 15:22
-
-
Save pingpoli/a91dc4e7ca45e813c9fde50ab2f5cef3 to your computer and use it in GitHub Desktop.
This file contains 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 | |
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