当前位置: 移动技术网 > IT编程>开发语言>PHP > ThinkPHP实现的rsa非对称加密类示例

ThinkPHP实现的rsa非对称加密类示例

2018年08月10日  | 移动技术网IT编程  | 我要评论

本文实例讲述了thinkphp实现的rsa非对称加密类。分享给大家供大家参考,具体如下:

公钥加密后的字符串是一直变化的,但是用私钥解密后的内容仍然是相同的,这是为了加密数据使用的。

私钥加密的字符串是不会变化的,即使暴露在外网上别人截取时如果没有公钥也是看不出来内容的,仅允许给予公钥的第三方来解密并看到内容,实际作用相当于签名功能,如果能拿到未加密的内容,说明一定是信任方的数据,因为有他的签名啊。

其实这种非对称加密技术可以用于单点登录中去,安全级别高,能解密获取到内容应该就是信任方的数据。

<?php
namespace common\org;
class rsacrypt {
 const cerpath ='../application/runtime/data/server.cer'; //生成证书路径
 const pfxpath = '../application/runtime/data/server.pfx'; //秘钥文件路径
 const filedir = '../application/runtime/data/';
  /**
  * 生成公钥私钥
  */
  public static function generatecertkey()
  {
  $dn = array('countryname'=>'cn', 'stateorprovincename'=>'beijing', 'localityname'=>'beijing','organizationname'=>'clcw',
    'organizationalunitname'=>'clcw', 'commonname'=>'clcw', 'emailaddress'=>'service@clcw.com.cn');
  $privkeypass = 'secret';  //私钥密码
  $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, self::pfxpath, $privkey, $privkeypass);
  (file_exists(self::cerpath)) or die('公钥的文件路径错误');
  (file_exists(self::pfxpath)) or die('密钥的文件路径错误');
  }
  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($appname='')
  {
  $result = ['status'=>0, 'msg'=>''];
  if (!extension_loaded('openssl') ) {
   $result['msg'] = 'php需要openssl支持';
  }
  //创建公钥
  $res = openssl_pkey_new();//array('private_key_bits'=>512) 这一串参数不加,否则只能加密54个长度的字符串
  //提取私钥
  openssl_pkey_export($res, $privatekey);
  //生成公钥
  $public_key = openssl_pkey_get_details($res);
  $publickey = $public_key['key'];
  // $path = self::filedir.$appname;
  try{
   // file_put_contents($path.'_public.pem', $publickey);
   // file_put_contents($path.'_private.pem', $privatekey);
   $result['status'] = 1;
   $result['publickey'] = $publickey;
   $result['privatekey'] = $privatekey;
  }catch(\exception $e) {
   // throw new \exception($e->getmessage());
   $result['msg'] = $e->getmessage();
  }
  return $result;
  }
  /**
  * 用私钥加密数据
  * @param $data string 需要加密的字符串(最好不要超过200个字符)
  * @param $appname string 应用名称
  */
  public static function privateencrypt($data, $appname)
  {
  $result = ['status'=>0, 'msg'=>''];
  $privatekey = c($appname.'.private_key');
  $myinfo = 'in '.__method__.',privatekey:'.$privatekey."\n";
  file_put_contents('/tmp/shiyf.log', $myinfo, file_append);
  //生成resource类型的密钥,如果密钥文件内容被破坏,openssl_pkey_get_private函数返回false
  $privatekey = openssl_pkey_get_private($privatekey);
  if (empty($privatekey)) {
   $result['msg'] = '密钥不可用';
  }
  $encryptdata = '';
  //用私钥加密
  if (openssl_private_encrypt($data, $encryptdata, $privatekey)) {
   $result['msg'] = base64_encode($encryptdata);
   $result['status'] = 1;
  } else {
   $result['msg'] = '加密失败!';
  }
  return $result;
  }
  /**
  * 用公钥解密数据
  * @param $data string 需要解密的字符串(最好不要超过200个字符)
  * @param $appname string 应用名称
  */
  public static function publicdecrypt($data, $appname)
  {
  $result = ['status'=>0, 'msg'=>''];
  $data = base64_decode($data);
  $publickey = c($appname.'.public_key');
  //生成resource类型的公钥,如果公钥文件内容被破坏,openssl_pkey_get_public函数返回false
  $publickey = openssl_pkey_get_public($publickey);
  if (empty($publickey)) {
   $result['msg'] = '公钥不可用';
  }
  //解密数据
  $decryptdata = '';
  if (openssl_public_decrypt($data, $decryptdata, $publickey)) {
   $result['msg'] = $decryptdata;
   $result['status'] = 1;
  } else {
   $result['msg'] = '解密失败';
  }
  return $result;
  }
  /**
  * 用公钥加密数据
  * @param $data string 需要加密的字符串(最好不要超过200个字符)
  * @param $appname string 应用名称
  */
  public static function publicencrypt($data, $publickey)
  {
  $result = ['status'=>0, 'msg'=>''];
  //生成resource类型的公钥,如果公钥文件内容被破坏,openssl_pkey_get_private函数返回false
  $publickey = openssl_pkey_get_public($publickey);
  if (empty($publickey)) {
   $result['msg'] = '公钥不可用';
  }
  $encryptdata = '';
  //用私钥加密
  if (openssl_public_encrypt($data, $encryptdata, $publickey)) {
   $result['msg'] = base64_encode($encryptdata);
   $result['status'] = 1;
  } else {
   $result['msg'] = '加密失败!';
  }
  return $result;
  }
  /**
  * 用私钥加密数据
  * @param $data string 需要解密的字符串(最好不要超过200个字符)
  * @param $appname string 应用名称
  */
  public static function privatedecrypt($data, $appname)
  {
  $result = ['status'=>0, 'msg'=>''];
  $data = base64_decode($data);
  $privatekey = c($appname.'.private_key');
  //生成resource类型的私钥,如果私钥文件内容被破坏,openssl_pkey_get_public函数返回false
  $privatekey = openssl_pkey_get_private($privatekey);
  if (empty($privatekey)) {
   $result['msg'] = '私钥不可用';
  }
  //解密数据
  $decryptdata = '';
  if (openssl_private_decrypt($data, $decryptdata, $privatekey)) {
   $result['msg'] = $decryptdata;
   $result['status'] = 1;
  } else {
   $result['msg'] = '解密失败';
  }
  return $result;
  }
}

ps:关于加密解密感兴趣的朋友还可以参考本站在线工具:

在线rsa加密/解密工具:

文字在线加密解密工具(包含aes、des、rc4等):

在线散列/哈希算法加密工具:

在线md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160加密工具:

在线sha1/sha224/sha256/sha384/sha512加密工具:

更多关于thinkphp相关内容感兴趣的读者可查看本站专题:《thinkphp入门教程》、《thinkphp模板操作技巧总结》、《thinkphp常用方法总结》、《codeigniter入门教程》、《ci(codeigniter)框架进阶教程》、《zend framework框架入门教程》及《php模板技术总结》。

希望本文所述对大家基于thinkphp框架的php程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网