验证中...
rsa加密生成(加密通信)
Raw Copy
<?php
/**
* User: orzblankcat
* Date: 2019/1/25
* Time: 17:30
*/
//var_dump(openssl_get_cipher_methods()); //加密模式
//var_dump(openssl_get_curve_names()); //加密模式
//var_dump(openssl_get_md_methods()); //加密模式
$config = [
// "digest_alg" => 'sha512', //加密模式
"private_key_bits" => 4096, //字节数 512 1024 2048 4096 等
"private_key_type" => OPENSSL_KEYTYPE_RSA, //加密类型
"config" => "E:/phpStudy/PHPTutorial/Apache/conf/openssl.cnf",
"encrypt_key"=>true //私钥加密
];
$passphrase = '123456';//密码 当encrypt_key=>true必填
extension_loaded('openssl') or die('php需要openssl扩展支持');
$res = openssl_pkey_new($config);
//提取私钥
openssl_pkey_export($res, $private_key, $passphrase,$config);
//生成公钥
$public_key = openssl_pkey_get_details($res);
$public_key=$public_key["key"];
//显示数据
var_dump($private_key); //私钥
$pri = fopen("pri.key", "w") or die("Unable to open file!");
fwrite($pri, $private_key);
echo "<br>";
var_dump($public_key); //公钥
$pub = fopen("pub.key", "w") or die("Unable to open file!");
fwrite($pub, $public_key);
echo "<br>";
//要加密的数据
$data = "http://www.baidu.com";
//私钥加密后的数据
$pi_key = openssl_pkey_get_private($private_key,$passphrase);//这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id
$pu_key = openssl_pkey_get_public($public_key);//这个函数可用来判断公钥是否是可用的
var_dump($pi_key);
var_dump($pu_key);
//----私钥加密
openssl_private_encrypt($data,$encrypted,$pi_key);
$encrypted = base64_encode($encrypted);
echo "私钥加密后的数据:<br>";
var_dump($encrypted);
echo "<br>";
openssl_public_decrypt(base64_decode($encrypted), $decrypted, $pu_key);
echo "公钥解密后的数据:<br>";
var_dump($decrypted);
echo "<br>";
//----相反操作、公钥加密
openssl_public_encrypt($data, $encrypted, $pu_key);
$encrypted = base64_encode($encrypted);
echo "公钥加密后的数据:<br>";
var_dump($encrypted);
echo "<br>";
openssl_private_decrypt(base64_decode($encrypted), $decrypted, $pi_key);//私钥解密
echo "私钥解密后的数据:<br>";
var_dump($decrypted);
echo "<br>";
rsa加密生成类(加密通信)
Raw Copy
<?php
/**
* User: orzblankcat
* Date: 2019/1/25
* Time: 17:30
*/
class RsaClass
{
protected $config = [
// "digest_alg" => 'sha512', //加密模式
"private_key_bits" => 4096, //字节数 512 1024 2048 4096 等
"private_key_type" => OPENSSL_KEYTYPE_RSA, //加密类型
"config" => "E:/phpStudy/PHPTutorial/Apache/conf/openssl.cnf",
"encrypt_key"=>true //私钥加密
];
protected $passphrase = '51A6ci%FEX6WOB71nISgZ7aeo$le#u#2';//密码 当encrypt_key=>true必填
protected $pi_key='';
protected $pu_key='';
protected $priPath='pri.key';
protected $pubPath='pub.key';
public function __construct()
{
extension_loaded('openssl') or die('php需要openssl扩展支持');
if(!file_exists($this->pubPath)||!file_exists($this->priPath))
{
$this->createKey();
}
}
public function createKey()
{
$res = openssl_pkey_new($this->config);
//提取私钥
openssl_pkey_export($res, $private_key, $this->passphrase,$this->config);
//生成公钥
$public_key = openssl_pkey_get_details($res);
$public_key = $public_key["key"];
//显示数据
$pri = fopen($this->priPath, "w") or die("Unable to open file!");
fwrite($pri, $private_key);
$pub = fopen($this->pubPath, "w") or die("Unable to open file!");
fwrite($pub, $public_key);
fclose($pri);
fclose($pub);
}
/**
* 加密数据
*/
public function encryptKey($data)
{
$this->pu_key = openssl_pkey_get_public(file_get_contents($this->pubPath));
openssl_public_encrypt($data, $encrypted, $this->pu_key);
return base64_encode($encrypted);
}
/**
* 解密数据
*/
public function decodeKey($data)
{
$this->pi_key = openssl_pkey_get_private(file_get_contents($this->priPath),$this->passphrase);
openssl_private_decrypt(base64_decode($data), $decrypted, $this->pi_key);//私钥解密
return $decrypted;
}
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search