Skip to content

Instantly share code, notes, and snippets.

@Luavis
Last active December 23, 2015 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luavis/6655151 to your computer and use it in GitHub Desktop.
Save Luavis/6655151 to your computer and use it in GitHub Desktop.
RSA.php
<?php
$privateKeyPassphrase = "PASSWORD";
$privateKeyString = <<<PK
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,462A3423852255C0
-----END RSA PRIVATE KEY-----
PK;
$publicKeyString = <<<PK
-----BEGIN PUBLIC KEY-----
-----END PUBLIC KEY-----
PK;
$privateKey = openssl_pkey_get_private(array($privateKeyString, $privateKeyPassphrase));
$publicKey = openssl_pkey_get_public(array($publicKeyString, $privateKeyPassphrase));
if (!$privateKey) {
echo "Private key NOT OK\n";
}
if (!$publicKey) {
echo "Public key NOT OK\n";
}
function decrypt($message)
{
global $privateKey;
if (!openssl_private_decrypt(base64_decode($message), $decryptedWithPrivateFromPublic, $privateKey)) {
return null;
}
return ($decryptedWithPrivateFromPublic);
}
function encrypt($message)
{
global $publicKey;
if (!openssl_public_encrypt($message, $encryptedWithPublic, $publicKey)) {
return null;
}
return base64_encode($encryptedWithPublic);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment