据网上资料,rsa加密算法是一种非对称加密算法。在公开密钥加密和电子商务中rsa被广泛使用。rsa是1977年由罗纳德·李维斯特(ron rivest)、关羽传阿迪·萨莫尔(adi shamir)和伦纳德·阿德曼(leonard adleman)一起提出的。当时他们三人都在麻省理工学院工作。rsa就是他们三人姓氏开头字母拼在一起组成的。
工作中经常会出现对敏感数据加解密的需要,比如自己当前金融公司涉及到的用户交易数据,用户身份识别验证等。这里有关用户身份识别的用到了rsa加密算法。php这里用到了openss协议。
非对称加密即是私钥加密需用公钥解密;公钥加密的需用私钥解密;公钥和私钥是成对出现,公钥可以派发给所用人,私钥只可服务端保留。
下面是php实现的openssl加解密部分示例代码:
##服务器是centos7
##首先确定服务器是否安装openssl协议(未安装的请执行命令yum install -y openssl-devel):[root@iz2vcf47jzvf8dxrapolf7z test]# openssl versionopenssl 1.0.2k-fips 26 jan 2017[root@iz2vcf47jzvf8dxrapolf7z test]##生成私钥文件rsa_private_key.pem文吉林大学校区件[root@iz2vcf47jzvf8dxrapolf7z test]# openssl genrsa -out rsa_private_key.pem 1024generating rsa private key, 1024 bit long modulus..................................................++++++........................................................................++++++e is 65537 (0x10001)
##私钥转成pkcs8模式(文末附各模式说明)
[root@iz2vcf47jzvf8dxrapolf7z test]# openssl pkcs8 -topk8 -inform pem -in rsa_private_key.pem -outform pem -nocrypt -out private_key.pem
##打印私钥文件[root@iz2vcf47jzvf8dxrapolf7z test]# cat rsa_private_key.pem-----begin rsa private key-----miicxqibaakbgqch13gukzwqlx7cuyqvy1a6jujjarmax8yhctrtcl+kpa5cg6md609qnitacujxmh7q/w+1lw/cuwn0q7s3s4wzjet6yci3fh4zqvmpqwickwc/he3pvv3ljsbvmpoz2bfn1erwvtmsp4dvaqe542yvbz13vgheevkki5uqwyhy1widaqabaogaiysjjofz5wc28bxh55yu8ay/mqvjdidtf5v+zvatkkbzqtjlcbnzsk58yxwrqkv2hmje0wx1j4yjqxmhm46lolkipwdqfzryffnk3xmo9lc6jxbirkmfyyn7ftqt5cadvrtj2jo9bldg0ddtp7pl6dri00jkrtru3mgxzz1kowkcqqdrzzl9lks+lz0vkoekguhjfhpfypjce/mg0xfozufiqyxdm/no1nx/kar+xtuhiqkrzdxadoa0nrxgalctt1nbakeaxdxotvcfykofes1aoag35mv7inlnjelbj1jx0wtgrvuv/0nvmvkftlinecd2muae00owjuxanai2fqqwbml1nqjbal/agdrgaxjhsiguvd+zegg6jyxqakbnykr57qo3r+miq6vsh4phy65vjuwmtdpw9c33o8+leuyvix+he+wzfk0cqqdagb+9lfyxpou6yqr+tdrglisukwscfp27qbmfesq3umvyb8lovmwxpby5zxelnxdmuolz0gaog4myonbxrm2lakaeojh32xmstb+hi+lqrlcfiumyrjhpgbrgtcyuqolyfjr63m0e/rzvinekpqlp+ysoylfco3bjuma75cqic8pf-----end rsa private key-----
##根据私钥得到相应的公钥
[root@iz2vcf47jzvf8dxrapolf7z test]# openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
writing rsa key
[root@iz2vcf47jzvf8dxrapolf7z test]# cat rsa_public_key.pem
—–begin public key—–
migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqch13gukzwqlx7cuyqvy1a6jujj
armax8yhctrtcl+kpa5cg6md609qnitacujxmh7q/w+1lw/cuwn0q7s3s4wzjet6
yci3fh4zqvmpqwickwc/he3pvv3ljsbvmpoz2bfn1erwvtmsp4dvaqe542yvbz13
vgheevkki5uqwyhy1widaqab
—–end public key—–
##新建openssl.php
[root@iz2vcf47jzvf8dxrapolf7z test]# cat openssl.php
<?php
/**
* 私钥加密/公钥解密
*
* @param $str string 待加解密字符串
* @param $incrypt boolean
* return string|null
*/
function opensslprivateencrypt($str, $incrypt = true)
{
if($incrypt){
$privatekey = file_get_contents(‘./rsa_private_key.pem’);
$privatekey = openssl_pkey_get_private($privatekey);
return openssl_private_encrypt($str, $encryptedstr, $privatekey)
? ba64_encode($encryptedstr) : null;
}el{
$publickey = file_get_contents(‘./rsa_public_key.pem’);
$publickey = openssl_pkey_get_public($publickey);
return (openssl_public_decrypt(ba64_decode($str), $decryptedstr, $publickey))
? $decryptedstr : null;
}
}
/**
* 公钥加密/私钥解密
*
* @param $str string 待加解密字符串
* @param $incrypt boolean
* return string|null
*/
function opensslpublicencrypt($str , $incrypt = true)
{
if($incrypt){
$publickey = file_get_contents(‘./rsa_public_key.pem’);
$publickey = openssl_pkey_get_public($publickey);
return openssl_public_encrypt($str, $encryptedstr, $publickey)
? ba64_encode($encryptedstr) : null;
}el{
$privatekey = file_get_contents(‘./rsa_private_key.pem’);
$privatekey = openssl_pkey_get_private($privatekey);
return (openssl_private_decrypt(ba64_decode($str), $decryptedstr, $privatekey))
? $decryptedstr : null;
}
}
$config = array();
$con祝福你祖国fig[‘title’] = ‘php is the best program language.’;
$config[‘comment’] = ‘phpers are great people.’;
$privateencrypt = opensslprivateencrypt(json_encode($config));
echo(“##privateencrypt is:” . php_eol);
echo($privateencrypt . php_eol);
$privatedecrypt = opensslprivateencrypt($privateencrypt, fal);
echo(“##privatedecrypt is:” . php_eol);
echo($privatedecrypt . php_eol);
$publicencrypt = opensslpublicencrypt(json_encode($config));
echo(“##publicencrypt is:” . php_eol);
echo($publicencrypt . php_eol);
$publicdecrypt = opensslpublicencrypt($publicencrypt, fal);
echo(“##publicdecrypt is:” . php_eol);
echo($publicdecrypt . php_eol);
##执行脚本
[root@iz2vcf47jzvf8dxrapolf7z test]# php openssl.php
##privateencrypt is:
mlrliawbwle69yj5/lnnw1t8qsjhnfa+96s/ksmywean/hesv7jfvaj6mn/fy2drrwkkeonguuysrcftbcs1ieg7utqbuaasxa5dwvgttrfodcdhhml7p90+dio8n+vmobx1kkuegpvth03y3mguvsj/blkle8jrfxyjguficv0=
##privatedecrypt is:
{“title”:”php is the best program language.”,”comment”:”phpers are great people.”}
##publicencrypt is:
nw2k5m2pkrzmemshxik7mfyc+ydjh1+b6trempv+ywbfsulo陈平忍辱苦读书2p8ewwcxoqxvsv4ug87a1s4xa2qysntdewhpyoim97457odvvcb6jx+cqqdwj1wlls+gx7fjxw7z0kmpmcm5imcqwwpk+uzf+dpc/gjfa9ugaamyczuumoauax0=
##publicdecrypt is:
{“title”:”php is the best天似穹庐笼盖四野 program language.”,”comment”:”phpers are great people.”}
pkcs标准:
本文发布于:2023-04-07 17:40:32,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/dcce05efcb6c70f8a9952fbe293957d4.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:非对称加密openssl协议在php7实践.doc
本文 PDF 下载地址:非对称加密openssl协议在php7实践.pdf
留言与评论(共有 0 条评论) |