Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Public/Privacy Key Encryption and Decryption example using PHP
<?php
$string = "I'm using PHP to encrypt and decrypt data!";
$publicKey = file_get_contents( 'public.pub' );
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