php rsa加密解密实例 及签名验证-自己实践

Posted 与f

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php rsa加密解密实例 及签名验证-自己实践相关的知识,希望对你有一定的参考价值。

 

 

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/1
 * Time: 1:50
 */

//注意格式一定要有下面的标识符
$pub_key = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCjwmQtK4aYLSL/aOSH4g4fdTBT1JLzeRchbR6fMylOvTjGMh4IngxCwi7NAbTm8Edr02s7HXmo7oweLfqDRHvYPz7aH5Kt6gtjGzokfIVo6nN+3jDfoNBws+pPDaro5KbeIVO0kK16m+51yPS4R3lFF6bZcrGb+xq8A/QrPHxWNQIDAQAB
-----END PUBLIC KEY-----";
$pri_key = "-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCjwmQtK4aYLSL/aOSH4g4fdTBT1JLzeRchbR6fMylOvTjGMh4IngxCwi7NAbTm8Edr02s7HXmo7oweLfqDRHvYPz7aH5Kt6gtjGzokfIVo6nN+3jDfoNBws+pPDaro5KbeIVO0kK16m+51yPS4R3lFF6bZcrGb+xq8A/QrPHxWNQIDAQABAoGAemICn8dRlVTWPO7VK8ADMftQnLXXBOJQKQj1w6BmlJPRZD18OJB1NUcN1uQZoCWeGrUsBEfo7hko2j0eZQ+/RRCanxM34l0cHP4WQLVYzZ5jGcAyFl3f6Ra4MKELGD3daynfct8z5+XsnD7Fwg/kWNWZ8NJIKHICoqBrgF6wnJkCQQDZUBhAarJsMdebDPw/wj8ovTYZWJf0oqoL+FjDsJF4p8e0MDMIGmsBTPbv5wVaF+8/EFTQ7PDhD0oWRKBpeT6/AkEAwOmgUPXWmxi/TtwjFfX7290GXERjkCwGc5Yj8bVh4YdjPl2ijaFFrogvr3gCKFDd9AD/Oz5zKcrxSl4H5sZcCwJBAMiUuD3E/fkFrFduDeqf1YI52xRcBK4F8mToDq5ZbHxsiNUVZBUHpVrm+kqG9xaoXujbnx3UhaWGYkDZiSKxiasCQQCD7MEX3KcgdbIOqfjMgeX1G5fH7XTxGUpoLVrzZwlDBCVYdww9MvbGPpfttXI0Q+klfrEMwM5c3E5afyeEKE61AkA0m6sjb5ypwXMbXo5+uSEHkpL0Qqb87SCRVV/Bli7OJNuv9DrdwVO27AA192WxUoDfC23faeeETB1Su4M785U6
-----END RSA PRIVATE KEY-----";


$str = "这个是要加密的字符串888";

//这个例子是演示RSA加密
//非对称加密 分为 公钥和私钥
//通常 公钥加密 私钥解密,私钥加密,公钥解密

/*
 * 用到的加密函数
openssl_private_decrypt — Decrypts data with private key
openssl_private_encrypt — Encrypts data with private key
openssl_public_decrypt — Decrypts data with public key
openssl_public_encrypt — Encrypts data with public key
 *
 */

//echo $private_key;
$pi_key =  openssl_pkey_get_private($pri_key);//这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id
$pu_key = openssl_pkey_get_public($pub_key);//这个函数可用来判断公钥是否是可用的
//print_r($pi_key);echo "\n";
//print_r($pu_key);echo "\n";


//公钥加密过程
$enStr = ‘‘;
openssl_public_encrypt($str,$enStr,$pu_key);
//加密后的字符 有些无法显示 需要 base64_encode ( string $data ) ,解密的时候不需要要 base64_decode ( string $data )
echo base64_encode ($enStr);
echo "<br/>";


//私钥解密过程
$deStr = ‘‘;
//$enStr = base64_decode ( $enStr);
openssl_private_decrypt($enStr,$deStr,$pi_key);
echo $deStr;
echo "<br/>";


//下面是签名验证例子
$binary_signature = "";
// At least with PHP 5.2.2 / OpenSSL 0.9.8b (Fedora 7)
// there seems to be no need to call openssl_get_privatekey or similar.
// Just pass the key as defined above
openssl_sign($str, $binary_signature, $pi_key, OPENSSL_ALGO_SHA1);

// Check signature
$ok = openssl_verify($str, $binary_signature, $pu_key, OPENSSL_ALGO_SHA1);
echo "check #1: ";
if ($ok == 1) {
    echo "signature ok (as it should be)\n";
} elseif ($ok == 0) {
    echo "bad (there‘s something wrong)\n";
} else {
    echo "ugly, error checking signature\n";
}

 

 

淘宝开放平台,安全签名就是用的此算法, 我自己(商家开发者)生成有商家私钥和商家公钥,支付宝那边有支付宝私钥和支付宝公钥。具体是商家把商家公钥给支付宝(支付宝用作解密和签名验证),我们获取支付宝公钥(用来对支付宝发来的信息进行解密和签名校验)。

注意,私钥一定是自己用的,公钥给其他人用.

 

参考地址 : http://php.net/manual/en/function.openssl-private-decrypt.php

https://docs.open.alipay.com/58/103242

 

以上是关于php rsa加密解密实例 及签名验证-自己实践的主要内容,如果未能解决你的问题,请参考以下文章

iOS RSA加密解密及签名验证

php RSA公钥私钥加解密和验证用法

RSA公私钥和签名、验签过程

使用RSA算法对接口参数签名及验签

RSA加密解密签名验签的原理及方法

RSA 加密及php实现