本文實(shí)例講述了ThinkPHP實(shí)現(xiàn)的rsa非對(duì)稱加密類。分享給大家供大家參考,具體如下:
公鑰加密后的字符串是一直變化的,但是用私鑰解密后的內(nèi)容仍然是相同的,這是為了加密數(shù)據(jù)使用的。
私鑰加密的字符串是不會(huì)變化的,即使暴露在外網(wǎng)上別人截取時(shí)如果沒(méi)有公鑰也是看不出來(lái)內(nèi)容的,僅允許給予公鑰的第三方來(lái)解密并看到內(nèi)容,實(shí)際作用相當(dāng)于簽名功能,如果能拿到未加密的內(nèi)容,說(shuō)明一定是信任方的數(shù)據(jù),因?yàn)橛兴暮灻 ?/p>
其實(shí)這種非對(duì)稱加密技術(shù)可以用于單點(diǎn)登錄中去,安全級(jí)別高,能解密獲取到內(nèi)容應(yīng)該就是信任方的數(shù)據(jù)。
?php
namespace Common\Org;
class RsaCrypt {
const CERPATH ='../Application/Runtime/Data/server.cer'; //生成證書(shū)路徑
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; //有效時(shí)長(zhǎng),單位為天
//生成證書(shū)
$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('公鑰的文件路徑錯(cuò)誤');
(file_exists(self::PFXPATH)) or die('密鑰的文件路徑錯(cuò)誤');
}
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個(gè)長(zhǎng)度的字符串
//提取私鑰
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 需要加密的字符串(最好不要超過(guò)200個(gè)字符)
* @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'] = '加密失??!';
}
return $result;
}
/**
* 用公鑰解密數(shù)據(jù)
* @param $data string 需要解密的字符串(最好不要超過(guò)200個(gè)字符)
* @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 需要加密的字符串(最好不要超過(guò)200個(gè)字符)
* @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 需要解密的字符串(最好不要超過(guò)200個(gè)字符)
* @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;
}
}
PS:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:
在線RSA加密/解密工具:
http://tools.jb51.net/password/rsa_encode
文字在線加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode
在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- 淺談PHP SHA1withRSA加密生成簽名及驗(yàn)簽
- PHP7.1實(shí)現(xiàn)的AES與RSA加密操作示例
- PHP封裝的非對(duì)稱加密RSA算法示例
- 基于PHP RSA密文過(guò)長(zhǎng)加密解密 越過(guò)1024的解決方法
- PHP實(shí)現(xiàn)RSA加解密算法示例(生成密鑰位數(shù)為1024位的方法)
- PHP的RSA加密解密方法以及開(kāi)發(fā)接口使用
- PHP檢測(cè)接口Traversable用法詳解
- PHP實(shí)現(xiàn)的MD5結(jié)合RSA簽名算法實(shí)例
- PHP實(shí)現(xiàn)RSA簽名生成訂單功能【支付寶示例】
- php rsa 加密,解密,簽名,驗(yàn)簽詳解
- php基于openssl的rsa加密解密示例
- PHP rsa加密解密使用方法
- php實(shí)現(xiàn)RSA加密類實(shí)例
- 詳解PHP使用非對(duì)稱加密算法RSA