Skip to content

Instantly share code, notes, and snippets.

@joubertredrat
Created August 9, 2019 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joubertredrat/ff7e719aa46849ccd232e415056bb2cc to your computer and use it in GitHub Desktop.
Save joubertredrat/ff7e719aa46849ccd232e415056bb2cc to your computer and use it in GitHub Desktop.
encrypt decrypt test
<?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