Skip to content

Instantly share code, notes, and snippets.

Created September 9, 2015 14:16
What would you like to do?
Public/Privacy Key Encryption and Decryption example using PHP
$string = "I'm using PHP to encrypt and decrypt data!";
$publicKey = file_get_contents( '' );
openssl_public_encrypt( $string, $encrypted, $publicKey );
$encrypted = base64_encode( $encrypted );
echo '<h1>Encrypted String</h1>';
echo $encrypted;
$privKey = file_get_contents( 'private.key' );
openssl_private_decrypt( base64_decode( $encrypted ), $decrypted, $privKey );
echo '<h1>Decrypted String</h1>';
echo $decrypted;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment