Last active
December 23, 2015 15:19
-
-
Save Luavis/6655151 to your computer and use it in GitHub Desktop.
RSA.php
This file contains hidden or 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 | |
$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