note 30313 added to function.openssl-public-encrypt
| From: | webmaster at costarica-travelinfo dot com | Date: | Thu, 13 Mar 2003 20:51:26 +0000 |
| Subject: | note 30313 added to function.openssl-public-encrypt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-45228@lists.php.net to get a copy of this message | ||
This example worked for me:
RedHat 7.2 / php 4.2.2 / Apache 1.3.7
// STEP 1: Encryption with Public Key (you will need Private Key to decrypt - see step2).
$string="Some Important Data";
$fp=fopen ("cert.pem","r");
$pub_key=fread ($fp,8192);
fclose($fp);
$PK="";
$PK=openssl_get_publickey($pub_key);
if (!$PK) {
echo "Cannot get public key";
}
$finaltext="";
openssl_public_encrypt($string,$finaltext,$PK);
if (!empty($finaltext)) {
openssl_free_key($PK);
echo "Encryption OK!";
}else{
echo "Cannot Encrypt";
}
// STEP 2: Decription (Using Private Key)
$fp=fopen ("pk.pem","r");
$priv_key2=fread ($fp,8192);
fclose($fp);
$PK2=openssl_get_privatekey($priv_key2);
$Crypted=openssl_private_decrypt($Data,$Decrypted,$PK2);
if (!$Crypted) {
$MSG.="<p class='error'>Cannot Decrypt ($CCID).</p>";
}else{
echo "Decrypted Data: " . $Decrypted;
}
--
http://www.php.net/manual/en/function.openssl-public-encrypt.php
http://master.php.net/manage/user-notes.php?action=edit+30313
http://master.php.net/manage/user-notes.php?action=delete+30313
http://master.php.net/manage/user-notes.php?action=reject+30313