怎么在ThinkPHP中实现一个rsa非对称加密类

  介绍

怎么在ThinkPHP中实现一个rsa非对称加密类?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

& lt; php ?   namespace 常见\ Org;   class  RsaCrypt  {   ,const  CERPATH =& # 39; . ./应用程序/运行/数据/server.cer& # 39;,,//生成证书路径   ,const  PFXPATH =, & # 39; . ./应用程序/运行/数据/server.pfx& # 39;,,//秘钥文件路径   ,const  FILEDIR =, & # 39; . ./应用程序/运行/数据/& # 39;;   ,/* *   *,才能生成公钥私钥   ,*/public 才能static  function  generateCertKey ()   {才能   dn 美元;才能=,阵列(& # 39;countryName& # 39;=祝辞& # 39;cn # 39;,, & # 39; stateOrProvinceName& # 39;=祝辞& # 39;北京# 39;,,& # 39;localityName& # 39;=祝辞& # 39;北京# 39;,& # 39;organizationName& # 39;=祝辞& # 39;clcw& # 39;,   ,,,& # 39;organizationalUnitName& # 39;=祝辞& # 39;clcw& # 39;,, & # 39; commonname # 39;=祝辞& # 39;clcw& # 39;,, & # 39; emailaddress # 39;=祝辞& # 39;service@clcw.com.cn& # 39;);   时间=美元才能privkeypass  & # 39;秘密# 39;;,,//私钥密码   时间=美元才能numberOfDays  365;,,,//有效时长,单位为天//生才能成证书   时间=美元才能privkey  openssl_pkey_new ();   时间=美元才能csr  openssl_csr_new ($ dn, privkey美元);=美元才能sscert  openssl_csr_sign (csr美元,null,, privkey美元,美元numberOfDays);   openssl_x509_export_to_file才能(sscert美元,,self:: CERPATH);   openssl_pkcs12_export_to_file才能(sscert美元,,自我::PFXPATH, privkey美元,美元privkeypass);   (才能file_exists (self:: CERPATH)),或是死(& # 39;公钥的文件路径错误& # 39;);   (才能file_exists (self:: PFXPATH)),或是死(& # 39;密钥的文件路径错误& # 39;);   ,,}   public 才能static  function  verifyData (originData美元,,decryptData美元)   {才能   时间=美元才能cer_key  file_get_contents (self:: $ cerpath);   时间=美元才能cer  openssl_x509_read ($ cer_key);=美元才能res  openssl_verify (originData美元,decryptData美元,美元cer);   var_dump (res)美元才能;   ,,}   ,/* *   *,才能生成公钥私钥文件   *,才能@param  appName 美元;string 应用名称   ,*/public 才能static  function  generateKey($浏览器名称=& # 39;& # 39;)   {才能   时间=美元才能result [& # 39;状态# 39;=祝辞0,,& # 39;味精# 39;=祝辞& # 39;& # 39;];   if 才能;(! extension_loaded (& # 39; openssl # 39;),), {   美元,,结果[& # 39;味精# 39;],=,& # 39;php需要openssl支持& # 39;;   ,,}//才能创建公钥   时间=美元才能res  openssl_pkey_new();//数组(& # 39;private_key_bits& # 39;=祝辞;512),这一串参数不加,否则只能加密54个长度的字符串//才能提取私钥   openssl_pkey_export才能(res美元,,privatekey美元);//生才能成公钥   时间=美元才能public_key  openssl_pkey_get_details (res);   时间=美元才能publickey 美元public_key[& # 39;关键# 39;];//才能,path 美元;=,self:: FILEDIR。$浏览器名称;   尝试才能{//,,,用file_put_contents()美元路径强生# 39;_public.pem& # 39;,, publickey美元);//,,,用file_put_contents()美元路径强生# 39;_private.pem& # 39;,, privatekey美元);   美元,,结果[& # 39;状态# 39;],=,1;   美元,,结果[& # 39;publickey& # 39;],=, publickey美元;   美元,,结果[& # 39;privatekey& # 39;],=, privatekey美元;   }才能赶上(\ Exception  $ e), {   ,,//,throw  new  \异常(e→美元getMessage ());   美元,,结果[& # 39;味精# 39;],=,e→美元getMessage ();   ,,}   return 才能;美元的结果;   ,,}   ,/* *   *,才能用私钥加密数据   *,才能@param  data 美元;string 需要加密的字符串(最好不要超过200个字符)   *,才能@param  appName 美元;string 应用名称   ,*/public 才能static  function  privateEncrypt(数据,美元,美元浏览器名称)   {才能   时间=美元才能result [& # 39;状态# 39;=祝辞0,,& # 39;味精# 39;=祝辞& # 39;& # 39;];   时间=美元才能privatekey  C(浏览器名称# 39;公司美元.PRIVATE_KEY& # 39;);=美元才能myinfo  & # 39;拷贝& # 39;.__METHOD__强生# 39;,privatekey: & # 39;。美元privatekey干净\ n";   写入才能(& # 39;/tmp/shiyf.log& # 39;,, myinfo美元,FILE_APPEND);//生才能成资源类型的密钥,如果密钥文件内容被破坏,openssl_pkey_get_private函数返回错误的   时间=美元才能privatekey  openssl_pkey_get_private ($ privatekey);   if 才能;(空(privatekey美元)),{   美元,,结果[& # 39;味精# 39;],=,& # 39;密钥不可用& # 39;;   ,,}   时间=美元才能encryptData  & # 39; & # 39;;//才能用私钥加密   if 才能;(openssl_private_encrypt(数据,美元,encryptData美元,美元privatekey)), {   美元,,结果[& # 39;味精# 39;],=,base64_encode ($ encryptData);   美元,,结果[& # 39;状态# 39;],=,1;   ,,},{else    美元,,结果[& # 39;味精# 39;],=,& # 39;加密失败! & # 39;;   ,,}   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在ThinkPHP中实现一个rsa非对称加密类