美文网首页PHP经验分享
php SHA256WithRSA签名验签&加密解密

php SHA256WithRSA签名验签&加密解密

作者: 聚米01 | 来源:发表于2019-10-22 17:30 被阅读0次

使用:

$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扩展

相关文章

网友评论

    本文标题:php SHA256WithRSA签名验签&加密解密

    本文链接:https://www.haomeiwen.com/subject/qrxcvctx.html