Created
August 9, 2019 21:21
-
-
Save joubertredrat/ff7e719aa46849ccd232e415056bb2cc to your computer and use it in GitHub Desktop.
encrypt decrypt test
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 | |
$publicKeyString = "-----BEGIN PUBLIC KEY----- | |
-----END PUBLIC KEY-----"; | |
$privateKeyString = "-----BEGIN RSA PRIVATE KEY----- | |
-----END RSA PRIVATE KEY-----"; | |
$publicKey = openssl_pkey_get_public($publicKeyString); | |
$privateKey = openssl_pkey_get_private($privateKeyString); | |
$data = '{"foo":"bar"}'; | |
// encode | |
$crypted = null; | |
$result = openssl_public_encrypt($data, $crypted, $publicKey); | |
$cryptedBase64 = base64_encode($crypted); | |
// decode | |
$decrypted = null; | |
$hash = base64_decode($cryptedBase64); | |
openssl_private_decrypt($hash, $decrypted, $privateKey); | |
var_dump($decrypted); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment