Forked from veny/gist:6c72c7bf89fd464b28c7512d2c472a5e
Created
July 18, 2018 15:29
-
-
Save tiago-peres/c12a125313af528ba832330f1ff3857f to your computer and use it in GitHub Desktop.
PHP, RSA, Encrypt/Decrypt
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 | |
// generate private/public key as follows: | |
// > openssl genrsa -out private.pem 2048 | |
// > openssl rsa -in private.pem -outform PEM -pubout -out public.pem | |
$data = "String to encrypt"; | |
$privKey = openssl_pkey_get_private('file:///path/to/private.pem'); | |
$encryptedData = ""; | |
openssl_private_encrypt($data, $encryptedData, $privKey); | |
echo 'Encrypted: ' . $encryptedData; | |
$pubKey = openssl_pkey_get_public('file:///path/to/public.pem'); | |
$decryptedData = ""; | |
openssl_public_decrypt($encryptedData, $decryptedData, $pubKey); | |
echo "\n---\nDecrypted: " . $decryptedData; | |
echo "\n[OK]\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment