溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在ThinkPHP中實現(xiàn)一個rsa非對稱加密類

發(fā)布時間:2021-04-09 17:19:36 來源:億速云 閱讀:463 作者:Leah 欄目:開發(fā)技術(shù)

怎么在ThinkPHP中實現(xiàn)一個rsa非對稱加密類?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

<?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 應(yīng)用名稱
  */
  public static function generateKey($appName='')
  {
  $result = ['status'=>0, 'msg'=>''];
  if (!extension_loaded('openssl') ) {
   $result['msg'] = 'php需要openssl支持';
  }
  //創(chuàng)建公鑰
  $res = openssl_pkey_new();//array('private_key_bits'=>512) 這一串參數(shù)不加,否則只能加密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;
  }
  /**
  * 用私鑰加密數(shù)據(jù)
  * @param $data string 需要加密的字符串(最好不要超過200個字符)
  * @param $appName string 應(yīng)用名稱
  */
  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類型的密鑰,如果密鑰文件內(nèi)容被破壞,openssl_pkey_get_private函數(shù)返回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'] = '加密失?。?#39;;
  }
  return $result;
  }
  /**
  * 用公鑰解密數(shù)據(jù)
  * @param $data string 需要解密的字符串(最好不要超過200個字符)
  * @param $appName string 應(yīng)用名稱
  */
  public static function publicDecrypt($data, $appName)
  {
  $result = ['status'=>0, 'msg'=>''];
  $data = base64_decode($data);
  $publickey = C($appName.'.PUBLIC_KEY');
  //生成resource類型的公鑰,如果公鑰文件內(nèi)容被破壞,openssl_pkey_get_public函數(shù)返回false
  $publickey = openssl_pkey_get_public($publickey);
  if (empty($publickey)) {
   $result['msg'] = '公鑰不可用';
  }
  //解密數(shù)據(jù)
  $decryptData = '';
  if (openssl_public_decrypt($data, $decryptData, $publickey)) {
   $result['msg'] = $decryptData;
   $result['status'] = 1;
  } else {
   $result['msg'] = '解密失敗';
  }
  return $result;
  }
  /**
  * 用公鑰加密數(shù)據(jù)
  * @param $data string 需要加密的字符串(最好不要超過200個字符)
  * @param $appName string 應(yīng)用名稱
  */
  public static function publicEncrypt($data, $publickey)
  {
  $result = ['status'=>0, 'msg'=>''];
  //生成resource類型的公鑰,如果公鑰文件內(nèi)容被破壞,openssl_pkey_get_private函數(shù)返回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;
  }
  /**
  * 用私鑰加密數(shù)據(jù)
  * @param $data string 需要解密的字符串(最好不要超過200個字符)
  * @param $appName string 應(yīng)用名稱
  */
  public static function privateDecrypt($data, $appName)
  {
  $result = ['status'=>0, 'msg'=>''];
  $data = base64_decode($data);
  $privatekey = C($appName.'.PRIVATE_KEY');
  //生成resource類型的私鑰,如果私鑰文件內(nèi)容被破壞,openssl_pkey_get_public函數(shù)返回false
  $privatekey = openssl_pkey_get_private($privatekey);
  if (empty($privatekey)) {
   $result['msg'] = '私鑰不可用';
  }
  //解密數(shù)據(jù)
  $decryptData = '';
  if (openssl_private_decrypt($data, $decryptData, $privatekey)) {
   $result['msg'] = $decryptData;
   $result['status'] = 1;
  } else {
   $result['msg'] = '解密失敗';
  }
  return $result;
  }
}

關(guān)于怎么在ThinkPHP中實現(xiàn)一個rsa非對稱加密類問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI