RSA加解密

PHP cooljun 203℃ 0评论

文件秘钥

/**
 * RSA签名
 * @param $data 待签名数据(按照文档说明拼成的字符串)
 * @param $private_key_path 商户私钥文件路径
 * return 签名结果
 */
function rsaSign($data) {
    $data = ConnectParams($data);
    $private_key_path = APP_URL."key/php_prkey.pem";
    $priKey = file_get_contents($private_key_path);
    $res = openssl_get_privatekey($priKey);
    openssl_sign($data, $sign, $res);
    openssl_free_key($res);
    //base64编码
    $sign = base64_encode($sign);
    return $sign;
}

/**
 * RSA验签
 * @param $data 待签名数据(文档中<plain>标签的值,包含<plain>标签)
 * @param $ali_public_key_path 富友的公钥文件路径
 * @param $sign 要校对的的签名结果
 * return 验证结果
 */
function rsaVerify($data, $sign)  {
    $data = ConnectParams($data);
    $ali_public_key_path = APP_URL."key/php_pbkey.pem";
    $pubKey = file_get_contents($ali_public_key_path);
    $res = openssl_get_publickey($pubKey);
    $result = (bool)openssl_verify($data, base64_decode($sign), $res);
    openssl_free_key($res); 
    return $result;
}

/**
 * [ConnectParams 拼接请求参数]
 */
function ConnectParams($data){
    ksort($data);
    $i = 1;
    foreach ($data as $key => $value) {
        if($i==1){
            $params = $value;
        }else{
            $params = $params."|".$value;
        }
        $i++;
    }
    return $params;
}

字符串秘钥

$str = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIb5OhXP3oMJxwT8d3pR8pR9JX4TFcy7UcjMGPTb2yPwt/Vnr2qOy3VrqYTDltr4euipVaJlIzdMmgShDkK4CMgHo2C38vzheZYsJWdT89H6HVKBX0tF+8p70GfkhUVo7icXTKuT3aCFhhI75fqx611CZ40q/TakRKtaDhra5hp1AgMBAAECgYAsyTHe3vSkYnApVWSHr/1gDA6OwOJw5o8Co8Z8KR+T02IXCeqI3wzfz3OWXBgIXH7cCKQjmI+Q39gTRSWoOcyfpuaBXJ7LNTbAhW4/gwI7Yscfe3NsKE/Zq5H836M489Q7R9J1w5vEegkWoLkdFvxTZGfnUGg//vDFeu1O05pXGQJBAM75us6OOWaEwHe7tp/cYH/A2uvXf1aIJdiQrxvUobV0hjzRGo2IRXW3CFEkOpNEreE7LlRKxR2sFrOrGZHxaYsCQQCm8YzS0ALWr7wQBAOEuRJws7JYNbOL26/N/H8yx9jyTTXRuice2UxlU+ST216AuJkZCoRFVotXEvXYbuXeTwv/AkBW7/7PlF33+fr+UBK9w0ulXdSXC8WpCzcaaGulPQNPvMpF8W890+eZMnCD5LvV8Bs5aCplraJufEJs5YmwC79RAkBFxrAVbNvC37C7APJ3huMuN+1S8R8aVbglYGPJ1b3XiG32OtgfDLPhpi7jncziAEX9MWgXkxSqUEJxM+jgAioBAkBepT9cxmwRA2q/7kAylrcVvsCaPZp79HsbbPAcbzLHYO9zv5GpvxK9QqLc+kbPKoJ4GnamTjcQSofFxATazxIS";
$str = chunk_split($str, 64, "\n");
$key = "-----BEGIN RSA PRIVATE KEY-----\n$str-----END RSA PRIVATE KEY-----\n";

转载请注明:cooljun小站 » RSA加解密

如果你觉得这篇文章对你有帮助,请支持我继续更新网站 !捐赠本站
喜欢 (0)or分享 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址