Last active
February 27, 2021 17:57
-
-
Save veny/6c72c7bf89fd464b28c7512d2c472a5e 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"; |
it should switch between position of private key and public key
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you have it backwards sir. Public key is for encryption and private key is for decryption.