使用:
$pri_key ="";
$pub_key = "";
$char = '方方块儿';//要加密的字符
$sign = $this->RsaEncrypt($char,$pri_key);//加密结果
$result = $this->RsaDecrypt($sign,$pub_key);//对加密结果进行解密
加密:
public function RsaEncrypt($str,$pri_key){
$pi_key =openssl_pkey_get_private($pri_key);
if(!$pi_key)return false;//秘钥不可用
openssl_private_encrypt($str,$encrypted,$pi_key);
$encrypted =base64_encode($encrypted);
return $encrypted;
}
解密:
public function RsaDecrypt($str,$pub_key){
$pu_key =openssl_pkey_get_public($pub_key);
if(!$pu_key)return false;//秘钥不可用
openssl_public_decrypt(base64_decode($str),$decrypted,$pu_key);
return $decrypted;
}
注:开启PHP的php_openssl扩展
网友评论