note 91210 added to book.openssl
| From: | bdh dot hall at gmail dot com | Date: | Fri, 29 May 2009 22:18:09 +0000 |
| Subject: | note 91210 added to book.openssl | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156170@lists.php.net to get a copy of this message | ||
I was having a heck of a time finding help on making asynchronous encryption/decryption using
private key/public key systems working, and I had to have it for creating a credit card module that
uses recurring billing.
You'd be a fool to use normal, 'synchronous' or two-way encryption for this, so the
whole mcrypt library won't help.
But, it turns out OpenSSL is extremely easy to use...yet it is so sparsely documented that it seems
it would be incredibly hard.
So I share my day of hacking with you - I hope you find it helpful!
<?php
if (isset($_SERVER['HTTPS']) )
{
echo "SECURE: This page is being accessed through a secure
connection.<br><br>";
}
else
{
echo "UNSECURE: This page is being access through an unsecure
connection.<br><br>";
}
// Create the keypair
$res=openssl_pkey_new();
// Get private key
openssl_pkey_export($res, $privatekey);
// Get public key
$publickey=openssl_pkey_get_details($res);
$publickey=$publickey["key"];
echo "Private Key:<BR>$privatekey<br><br>Public
Key:<BR>$publickey<BR><BR>";
$cleartext = '1234 5678 9012 3456';
echo "Clear text:<br>$cleartext<BR><BR>";
openssl_public_encrypt($cleartext, $crypttext, $publickey);
echo "Crypt text:<br>$crypttext<BR><BR>";
openssl_private_decrypt($crypttext, $decrypted, $privatekey);
echo "Decrypted text:<BR>$decrypted<br><br>";
?>
Many thanks to other contributors in the docs for making this less painful.
Note that you will want to use these sorts of functions to generate a key ONCE - save your
privatekey offline for decryption, and put your public key in your scripts/configuration file. If
your data is compromised you don't care about the encrypted stuff or the public key, it's
only the private key and cleartext that really matter.
Good luck!
----
Server IP: 64.71.164.2
Probable Submitter: 208.73.117.34
----
Manual Page -- http://www.php.net/manual/en/book.openssl.php
Edit -- https://master.php.net/note/edit/91210
Del: integrated -- https://master.php.net/note/delete/91210/integrated
Del: useless -- https://master.php.net/note/delete/91210/useless
Del: bad code -- https://master.php.net/note/delete/91210/bad+code
Del: spam -- https://master.php.net/note/delete/91210/spam
Del: non-english -- https://master.php.net/note/delete/91210/non-english
Del: in docs -- https://master.php.net/note/delete/91210/in+docs
Del: other reasons-- https://master.php.net/note/delete/91210
Reject -- https://master.php.net/note/reject/91210
Search -- https://master.php.net/manage/user-notes.php