Download file
Line 2 : "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Line 3 : <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> Line 4 : Line 5 : <html> Line 6 : <head> Line 7 : <title>Mobilefish.com - Encrypt and decrypt a message using the cerificate and private key file</title> Line 8 : </head> Line 9 : Line 10 : <body> Line 11 : <?php Line 12 : // Enter the location of your openssl.cnf file Line 13 : DEFINE("OPEN_SSL_CONF_PATH", "C:/tools/php-5.2.8-Win32/extras/openssl/openssl.cnf"); Line 14 : Line 15 : // Enter your private key and certificate file location Line 16 : $key = "c:/mobilefish_web/zs84198_key.pem"; // Absolute location of private key Line 17 : $cert = "c:/mobilefish_web/zs84198_cert.pem"; // Absolute location of certificate file Line 18 : Line 19 : // If your private key is protected by a passphrase Line 20 : $passphrase = "mysecret"; // If your private key is not protected: $passphrase = ""; Line 21 : Line 22 : // Enter the message which will be encrypted and decrypted Line 23 : $message = "Welcome to mobilefish.com The web, programming, iot and blockchain developers resource."; Line 24 : echo "Text: ".$message; Line 25 : echo "<br /><br />"; Line 26 : Line 27 : // ====== Encrypt message ====== Line 28 : $fp=fopen($key,"r"); Line 29 : $priv_key=fread($fp,8192); Line 30 : fclose($fp); Line 31 : if($passphrase == "" || $passphrase == null) { Line 32 : $res = openssl_get_privatekey($priv_key); Line 33 : } else { Line 34 : $res = openssl_get_privatekey($priv_key,$passphrase); Line 35 : } Line 36 : Line 37 : openssl_private_encrypt($message,$crypttext,$res); Line 38 : echo "Text encrypted: $crypttext"; Line 39 : echo "<br /><br />"; Line 40 : Line 41 : // ====== Get public key from certificate file ====== Line 42 : $fp=fopen ($cert,"r"); Line 43 : $pub_key=fread($fp,8192); Line 44 : fclose($fp); Line 45 : openssl_get_publickey($pub_key); Line 46 : Line 47 : // ====== Decrypt message ====== Line 48 : openssl_public_decrypt($crypttext,$newmessage,$pub_key); Line 49 : echo "Text decrypted: $newmessage"; Line 50 : echo "<br /><br />"; Line 51 : ?> Line 52 : Line 53 : </body> Line 54 : </html> |