<?php
class Rsa
{
private $privateKeyFilePath = 'rsa_private_key.pem';
private $publicKeyFilePath = 'rsa_public_key.pem';
// 加密以后的数据
private $encryptData = '';
private function test()
{
extension_loaded('openssl') or die('php需要openssl扩展支持');
(file_exists($this->privateKeyFilePath) && file_exists($this->privateKeyFilePath)) or die('文件路径不正确');
$privateKey = openssl_pkey_get_private(file_get_contents($this->privateKeyFilePath));
$publicKey = openssl_pkey_get_public(file_get_contents($this->publicKeyFilePath));
($privateKey && $publicKey) or die('密钥或者公钥不可用');
}
/**RSA签名函数
* $data为待签名数据,比如URL
* 签名用游戏方的保密私钥,必须是没有经过pkcs8转换的.结果需要用base64编码以备在URL中传输
* return Sign 签名
*/
public static function sign($data)
{
$priKey = file_get_contents('rsa_private_key.pem');
//转换为openssl密钥,必须是没有经过pkcs8转换的私钥
$res = openssl_get_privatekey($priKey);
//调用openssl内置签名方法,生成签名$sign
openssl_sign($data, $sign, $res);
openssl_free_key($res);
$sign = base64_encode($sign);
return $sign;
}
/**RSA验证签名
* $data为要验证的数据字符串
* $sign是需要验证的签名数据,是直接从URL中取到的$_POST["sign"]型参数,函数里面会进行base64_decode的。
* return 验签是否通过,为BOOL值
*/
public static function verify($data, $sign)
{
//读取公钥文件,也就是签名方公开的公钥,用来验证这个data是否真的是签名方发出的
$pubKey = file_get_contents('key/rsa_public_key.pem');
$res = openssl_get_publickey($pubKey);
//调用openssl内置方法验签,返回bool值
$result = (bool)openssl_verify($data, base64_decode($sign), $res);
openssl_free_key($res);
return $result;
}
public function encrypt($originalData)
{
$encryptData = '';
// echo '原数据为:', $originalData, PHP_EOL;
///用私钥加密
if (openssl_private_encrypt($originalData, $encryptData, openssl_pkey_get_private(file_get_contents($this->privateKeyFilePath)))) {
// 加密后 可以base64_encode后方便在网址中传输
// echo '加密成功,加密后数据(base64_encode后)为:', base64_encode($encryptData), PHP_EOL;
return ($encryptData);
} else {
// exit('加密失败');
return false;
}
}
public function decrypt($encryptData)
{
//用公钥解密
//解密以后的数据
$decryptData = '';
if (openssl_public_decrypt($encryptData, $decryptData, openssl_pkey_get_public(file_get_contents($this->publicKeyFilePath)))) {
return ($decryptData);
} else {
return false;
}
}
/**
* 用私密钥加密
*/
public function private_encrypt($input)
{
openssl_private_encrypt($input, $output, file_get_contents($this->privateKeyFilePath));
return base64_encode($output);
}
/**
* 解密 私密钥加密后的密文
*/
public function public_decrypt($input)
{
openssl_public_decrypt(base64_decode($input), $output, file_get_contents($this->publicKeyFilePath));
return $output;
}
/**
* 用公密钥加密
*/
public function public_encrypt($input)
{
openssl_public_encrypt($input, $output, $this->public_key_resource);
return base64_encode($output);
}
/**
* 解密 公密钥加密后的密文
*/
public function private_decrypt($input)
{
openssl_private_decrypt(base64_decode($input), $output, $this->private_key_resource);
return $output;
}
}
//$Rsa = new Rsa;
//$encrypt =$Rsa->encrypt($Rsa->originalData);
//$decrypt = $Rsa->decrypt($encrypt);
//var_dump( base64_encode($encrypt), $decrypt);
//die;
php Rsa加密
猜你喜欢
转载自blog.csdn.net/qq_38324424/article/details/114820291
今日推荐
周排行