基础代码
<?php /** * @author: suifengtec coolwp.com * @date: 2015-07-24 09:22:18 * @last Modified by: suifengtec coolwp.com * @last Modified time: 2015-07-24 10:22:00 */ final class Encryption_a1{ protected static $_instance = null; protected static $private_key = null; protected static $public_pem = null; public static function instance() { if ( is_null( self::$_instance ) ) { self::$_instance = new self(); } return self::$_instance; } public function __clone() { _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'cwp' ), '0.9.0' ); } public function __wakeup() { _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'cwp' ), '0.9.0' ); } public function __construct() { if ( is_null( self::$public_pem ) ) { $public_pem = getcwd().'../_key/public.pem'; self::$public_pem = openssl_get_publickey( file_get_contents( $public_pem ) ); } if ( is_null( self::$private_key ) ) { $private_key = getcwd().'../_key/private.key'; self::$private_key = openssl_get_privatekey( file_get_contents( $private_key ) ); } } public function en($data){ openssl_seal($data, $encrypted, $e, array(self::$public_pem)); $r = array( 'pwd' => base64_encode($encrypted), 'data' => base64_encode($e[0]), ); return $r; } public function de($args){ if(!is_array($args)||!isset($args['data'])||!isset($args['pwd'])){ return false; } extract($args); $pwd = base64_decode($pwd); $data = base64_decode($data); $r = null; openssl_open($pwd, $r, $data, self::$private_key); return $r; } }/*//CLASS*/ $GLOBALS['Encryption_a1'] = Encryption_a1::instance(); ?>
使用
require_once(getcwd().'/class-m-1.php'); global $Encryption_a1; $m = $Encryption_a1; $str = '被加密的数据在这里!R#%$#^$#T^$#!!!!!'; echo '<br>要被加密的数据:<br> '.$str; $data1 = $m->en($str); echo '<br>加密后返回的数组:<br> '; print_r($data1); echo '<br>解密后:<br> '; $str1 = $m->de($data1); echo $str1;
同志们,信息加密传递迫在眉睫啊!