note 46321 added to function.openssl-public-encrypt

From: Date: Thu, 07 Oct 2004 01:37:23 +0000
Subject: note 46321 added to function.openssl-public-encrypt
Groups: php.notes 
Request: Send a blank email to php-notes+get-78105@lists.php.net to get a copy of this message
To store the encrypted data in a MySQL database, you first have to encode the data so it can safely be written. You can use a blob type for this, but it can make SELECTs really nasty. The easiest way I have found to do this is with base64_encode and base64_decode. The following example using code from a previous example and split into encrypt and decrypt functions. function EncryptData($source) { $fp=fopen("/etc/httpd/conf/ssl.crt/server.crt","r"); $pub_key=fread($fp,8192); fclose($fp); openssl_get_publickey($pub_key); /* * NOTE: Here you use the $pub_key value (converted, I guess) */ openssl_public_encrypt($source,$crypttext,$pub_key); return(base64_encode($crypttext)); } function DecryptData($source) { #print("number : $number"); $fp=fopen("/etc/httpd/conf/ssl.key/server.key","r"); $priv_key=fread($fp,8192); fclose($fp); // $passphrase is required if your key is encoded (suggested) $res = openssl_get_privatekey($priv_key,$passphrase); /* * NOTE: Here you use the returned resource value */ $decoded_source = base64_decode($source); openssl_private_decrypt($decoded_source,$newsource,$res); return($newsource); } Just use the return values to store the encrypted data or display the decrypted data. ---- Manual Page -- http://www.php.net/manual/en/function.openssl-public-encrypt.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46321 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46321&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+46321&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#78105) next »