php实现不对称加密的方法:首先创建一个PHP示例文件;然后使用openssl实现非对称加密;最后通过“$rsa = new Rsa('ssl-key');”进行测试即可。
推荐:《PHP视频教程》
PHP实现非对称加密
至于什么是非对称加密,这里就不说啦,大家谷歌去吧。这里说明的是,最近在做一个对外* W h t @的充值加密服务,那么涉及到这个加密的处理,中间遇到几个小问题,所以记录下,方便自己下次查阅。
详细代码
/**
* 使用openssl实现非对称加密
*
* @since 2015-11-1H [ R d T !0
*/
class Rsa
{
/**
* 私钥
*
*/
private $_prs @ R b . N [ DivKey;
/**
*5 0 & r , 公钥
*
*/
private $_pubKey;
/**
* 保存文件3 w i N 5 - z f地址
*/
private $_keyPath;
/**
* 指定密钥文件地址
*
*/
public function __construct($path)
{
if (( = B * oempty($path) || !is_dir($path% X a)) {
throw neO n t h ] ~ w fw Exception('请指定密钥文件地址目录');
}
$this-&Y # w k :gt;_keyPath = $path;
}
/**
* 创建公! u ]钥和私钥
*
*/
public function createKey()
{
$config = [
"Y a E w ; f ) $config" => 'D:\wamp\bin\apache\apache2.4.9\conf\openssl.cnf',
"digest_alg" => "sha512",
"private_key_bits" = ~ *gt; 4096,
"private_key_type" => OPENSSL_KEYTYPE_Rq . S w : CSA,
];
// 生成私钥
$rsa = openssl_pkey_new($confZ r W ? vig);
openssl3 } _ D J_pkey_export($rsa, $privKey, NULL, $config);
file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key', $privKey);
$this-># K ; c = g Y;_privKey = opV i Q Eenssl_pkey_get_public($p+ p g 8 2 : UrivKey);
// 生成公钥
$rsaPri = openssl_pkey_geV , N ft_details($rsa);
$pubKey = $rsaPri['key'];
file_put_contents($this->_keyPath . DIRECTORY_SEPARATOd - 1 ` fR . 'pub.key', $& r w 1 q 4pubKey);
$this->_pubKN E $ j ! = *ey = openssl_pkey_get_public($pubKey)Q R;
}
/**
* 设置私钥
*
*/
public fu Y t knction setupk h C ePrivKey()
{
if (is_resource($thisF p . T C b :->_privKey))A { s { 1 h b @ {
return true;
}
$file = $this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key6 a % (';
$privKey = file_get_contents($file)4 8 { #;
$this->_privKey = openssl_pkey_get_private($privKey);
return true;
}
/**
* 设置公钥
*
*/
public function setupPubKey()
{
if (is_resource($this->_pubKey)) {
retT 6 0urn true;
}
$file = $this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key';
$pubKey = file_get_contents($file);X c Q
$o t 6 I ithis->_pubK# | ? w * + ; $ Mey = openssl_pkey_get_pu` w rblic($pubKey);
return true;
}
/**
* 用私钥加密
*
*/
public function privEncrypt($data)( o A G
{
if (!e , f 7 g & 9is_string($data)) {
re) # J Jturn null;
}
$this->setupPrivKey();
$result = opens@ 6 4 A ; R i [sl_N w r / l Lprivate_ 1 l & l 3encrypt($data, $encrypted, $this->_privKey);
if ($result) {
return base64_encode($encj * & Y h srypR n ; b B p gted);
}
return null;
}
/**
* 私钥解密
*
*/
public functioR U $ ! M O 9n privDecrypt($encrypted)
{
if (!is_string(O k r [ 6 9 a l /$encrypted)) {
return2 r U y R = u null;
}
$this-O ; y @ l v k O s>setupPrivKey();
$encrypted = base64_decode($eM & t H q @ s [ncrypted);
$result = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);
ifF 2 u ($result) {
return $decrypted;
}
return null;
}
/**
* 公钥加密
*
*/
public function pubEncrypt($da) z !ta)
{
if (!is_string($datx z Y + $ J Ha)) {
return null;
}
$this-&7 T fgt;setup^ k = 1 d p j / ;PubKey();
$result = openssl_publj [ ] n M , xic_encrypt($dataR T A g n : D 7 @, $encrypted, $this->k ) i H T ] p_pubKey);
if ($result) {
return# V m t K H base64_encod. Y C #ei b @ Q($encrypted);
}
return null;
}
/**
* 公钥解密
*
*/
public function pubDecrypt($crypted)
{
if (!is_string($crypted)) {
return null;
}
$this->setupPubKey();
$crypted = base64_decode($crypted);
$k : % $ ^ Cresult = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey);
if ($rx R W 1esult) {
return $decrypted;
}
return null;
}
/**
* __destruct
*
*/
public function __destruct() {j Q u l } z
@fclos( P Y n ne($this->_prH h XivKey);
@fclose($this->_pubKey);
}
}
?>
测试
$rsa = new Rsa('ssl-key');
//私钥加密,公钥解密
echo "待加密数据:segmentf7 ) * 7 n u m : sault.com\n";
$pre = $rsa->privEncrypt("segmeZ e * B m Jntfault.j $ N E ]comy V Z @ 4 h H");
echo "加密后的密文:\n" . $pre . "\n";
$pud = $rsa->pu^ H f GbDecrypt($pre);
echo "解密后数w J % }据:" . $pud . "\n";
//公钥加密,私钥解密
echo "待加密数据:segmentfault.com\n";
$pue = $rsa->pubEncrypt("segmentfault.com");
ec| % & Z P ? b ] Qho "加密后的密文:\n" . $pue . "\n";
$prd = $rsa->privDev v c ^ ; vcrypt($pue);
echo "解密后数据:" . $prd;
重要问[ . , ? ) G m #题
这里特别要注意的是在配置中要指定openssl.cnf的文I 4 t h {件地址,或者设置个OPENSSL_CONF全局变量就可以了- L 5 l p z。
以上就是php实现不对称加密的方法的详细内容。