Skip to content

Instantly share code, notes, and snippets.

@niraj-shah
Created September 9, 2015 14:16
Show Gist options
  • Save niraj-shah/060797d0eaecf98d9601 to your computer and use it in GitHub Desktop.
Save niraj-shah/060797d0eaecf98d9601 to your computer and use it in GitHub Desktop.
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