其实有时候觉得写博客好烦,就个函数就开篇博客。很小的意见事情而已,知道的人看来多取一举,或者说没什么必要,浪费时间,不知道的人就会很郁闷。技术就是这样的,懂的人觉得真的很简单啊,不知道的人真的好难。。。
一般在跟第三方接口对接数据的时候,为了保证很多都使用的RSA签名,没性趣了解的同学只需要知道原理的同学,主需要知道“RSA非对称加/解密算法中最流行最牛逼的然后知道怎么使用它就足够了”
重点内容
如果下面的链接没有被翔的话可以直接飞机过去,关于RSA加密算法的原理有兴趣的同学可以搜索“阮一峰”的博客
RSA加密算法原理一
RSA加密算法原理二
private static function sign($data){
$pk = self::$private_key;
openssl_sign($data, $sign, $pk);
$sign = base64_encode($sign);
return $sign;
}
咋一看$sign哪里来的???,微臣不知道啊,跟一般的php函数有点不一样,捂脸….
语法是这样的:
openssl_sign(“您要签名的数据”,”签名后返回来的数据”,”签名的钥匙/可以是公钥签名也可以是私钥签名,一般是私钥加密,公钥解密”)
函数解析:
sign() 函数里面的openssl_sign(data,data,sign,pk);传入了pk);传入了data数据 openssl_sign()对data进行签名,用的是静态变量data进行签名,用的是静态变量private_key,签名完成后返回了$sign 作为签名结果,然后再base64_encode() 进行二进制编码,然后返回编码过后的签名
<?php
$private_key = '-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA346nvWRmWhmdpHSsai6aUfDg9SehCUsDmBNji8OXmiQJmkBO
ssN/cu3Ifap7P6sgENzZbh/SYB/+i3JeBGlQetFt+kjJQnr1lDUQlSWBBq6OyxGT
LYDw3NyFsV1BN/NdQ06HzT1k/J0AZChyLMpMR8HUWZE6stbnocdY9aDXXAILyRa6
tJ6WUwepfMUFZfQAxWkpNLVXzY+QTf95ACblqVRoqRbVNNL4AOT1FYmQyIrD9gUI
wMQaUJ+2fGJtT9m1rbLpj87BddKKw5T7TOwqwDJrEgrHQ5m3YmphAEIrM6ZreV2C
FlT4RI8kvFfivqo/TvuTO4rMITzCwvxK6+dzuwIDAQABAoIBAQCRaFVcT6hvJEgw
Bp96dR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ie//P7OKXVTHQZUkv5bWKCBgYbukb1
ErpDGUZqjRY0uJ3NUspcdIAYFu7vqHk7/epT1Ev6qGb6v9tkM7iudtS7LBtsfSjx
Z8G0msgfQDur9pyEVb9L3Im5K5TrzUiDZt0s1eok6WrwpiLOjNMoEw==
-----END RSA PRIVATE KEY-----';
$public_key = '-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA346nvWRmWhmdpHSsai6a
UfDg9SehCUsDmBNji8OXmiQJmkBOssN/cu3Ifap7P6sgENzZbh/SYB/+i3JeBGlQ
etFt+kjJQnr1lDUQlSWBBq6OyxGTLYDw3NyFsV1BN/NdQ06HzT1k/J0AZChyLMpM
R8HUWZE6stbnocdY9aDXXAILyRa6tJ6WUwepfMUFZfQAxWkpNLVXzY+QTf95ACbl
qVRoqRbVNNL4AOT1FYmQyIrD9gUIwMQaUJ+2fGJtT9m1rbLpj87BddKKw5T7TOwq
wDJrEgrHQ5m3YmphAEIrM6ZreV2CFlT4RI8kvFfivqo/TvuTO4rMITzCwvxK6+dz
uwIDAQAB
-----END PUBLIC KEY-----';
//echo $private_key;
$pi_key = openssl_pkey_get_private($private_key);//这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id
$pu_key = openssl_pkey_get_public($public_key);//这个函数可用来判断公钥是否是可用的
echo $pi_key;echo "<hr>";
echo $pu_key;echo "<hr>";
$data = "<h3>你忙吧,我吃柠檬!</h3>";//原始数据
$encrypted = "";
$decrypted = "";
echo "---------------------------------------","私钥加密,用公钥解密:","---------------------------------------";
echo "source data:",$data,"\n";
echo "<hr>";
echo "加密内容:";
openssl_private_encrypt($data,$encrypted,$pi_key);//私钥加密
$encrypted = base64_encode($encrypted);//加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
echo $encrypted,"\n";
echo "<hr>";
echo "解密开始:";
openssl_public_decrypt(base64_decode($encrypted),$decrypted,$pu_key);//私钥加密的内容通过公钥可用解密出来
echo $decrypted,"\n";
echo "---------------------------------------","公钥加密,用私钥解密:","---------------------------------------"."<br>";
openssl_public_encrypt($data,$encrypted,$pu_key);//公钥加密
$encrypted = base64_encode($encrypted);
echo '加密内容:',$encrypted;
echo "<hr>";
echo "解密内容:";
openssl_private_decrypt(base64_decode($encrypted),$decrypted,$pi_key);//私钥解密
echo $decrypted,"\n";
如果你看到图片这样的输出,就没毛病了,说明你的公钥和私钥是一对的
介绍下钥匙生成(win下):openssl钥匙生成工具点击下载
下载文件后解压,我这里放在D:\openssl目录下
打开cmd控制台 ,就是cmd进入黑窗口啦,哈哈哈…
执行命令下面命令
d:
cd openssl/bin
先生成私钥,参数1024/2048可选择rsa_private_key.pem 这个是生成的文件名,可以自定义,建议不要有中文
执行:openssl genrsa -out rsa_private_key.pem 2048
#再根据私钥生成公钥文件
执行:openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
然后就躺着一对密匙了:
然后编译器可以打开它,不推荐记事本打开
定义变量
$PRIVATE_KEY = '-----BEGIN RSA PRIVATE KEY-----
************************
************************
************************
-----END RSA PRIVATE KEY-----';
温馨提示:开始和结束的标签都要保留的哦,不要扔掉咯!!!!!!!!!
---------------------
<?phpclass Md5RSA{/*** 利用约定数据和私钥生成数字签名* @param $data 待签数据* @return String 返回签名*/public function sign($data=''){if (empty($data)){return False;}$private_key = file_get_contents(dirname(__FILE__).'/rsa_private_key.pem');if (empty($private_key)){echo "Private Key error!";return False;}$pkeyid = openssl_get_privatekey($private_key);if (empty($pkeyid)){echo "private key resource identifier False!";return False;}$verify = openssl_sign($data, $signature, $pkeyid, OPENSSL_ALGO_MD5);openssl_free_key($pkeyid);return $signature;}/*** 利用公钥和数字签名以及约定数据验证合法性* @param $data 待验证数据* @param $signature 数字签名* @return -1:error验证错误 1:correct验证成功 0:incorrect验证失败*/public function isValid($data='', $signature=''){if (empty($data) || empty($signature)){return False;}$public_key = file_get_contents(dirname(__FILE__).'/rsa_public_key.pem');if (empty($public_key)){echo "Public Key error!";return False;}$pkeyid = openssl_get_publickey($public_key);if (empty($pkeyid)){echo "public key resource identifier False!";return False;}$ret = openssl_verify($data, $signature, $pkeyid, OPENSSL_ALGO_MD5);switch ($ret){case -1:echo "error";break;default:echo $ret==1 ? "correct" : "incorrect";//0:incorrectbreak;}return $ret;}}
原文:/qq_27517377/article/details/79047021
/kennyhr/p/3746100.html
如果觉得《php openssl_sign() 语法+RSA公私钥加密解密 非对称加密算法详解》对你有帮助,请点赞、收藏,并留下你的观点哦!