php rsa加密乱码_PHP实现RSA加密,解密,加签,验签

公钥用于对数据进行加密,私钥用于对数据进行解密;

私钥用于对数据进行签名,公钥用于对签名进行验证。

封装的RSA代码如下:

class Rsa

{

/**

* private key

*/

private $_privKey;

/**

* public key

*/

private $_pubKey;

/**

* the keys saving path

*/

private $_keyPath;

public function __construct ($path)

{

if (empty($path) || !is_dir($path)) {

throw new \Exception('Must set the keys save path');

}

//设置私钥

$this->_keyPath = $path;

$file = $this->_keyPath . DIRECTORY_SEPARATOR . 'rsa_private_key.pem';

$prk = file_get_contents($file);

$this->_privKey = openssl_pkey_get_private($prk);

//设置公钥

$file = $this->_keyPath . DIRECTORY_SEPARATOR . 'rsa_public_key.pem';

$puk = file_get_contents($file);

$this->_pubKey = openssl_pkey_get_public($puk);

}

/**

* setup the private key

*/

public function setupPrivKey ()

{

if (is_resource($this->_privKey)) {

return true;

}

$file = $this->_keyPath . DIRECTORY_SEPARATOR . 'rsa_private_key.pem';

$prk = file_get_contents($file);

$this->_privKey = openssl_pkey_get_private($prk);

return true;

}

/**

* setup the public key

*/

public function setupPubKey ()

{

if (is_resource($this->_pubKey)) {

return true;

}

$file = $this->_keyPath . DIRECTORY_SEPARATOR . 'rsa_public_key.pem';

$puk = file_get_contents($file);

$this->_pubKey = openssl_pkey_get_public($puk);

return true;

}

/**

* @function 私钥加密

* @param $data

* @return string|null

*/

public function privEncrypt ($data)

{

if (!is_string($data)) {

return null;

}

$r = openssl_private_encrypt($data, $encrypted, $this->_privKey);

if ($r) {

return base64_encode($encrypted);

}

return null;

}

/**

* @function 私钥解密

* @param $data

* @return string|null

*/

public function privDecrypt ($encrypted)

{

if (!is_string($encrypted)) {

return null;

}

$encrypted = base64_decode($encrypted);

$r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);

if ($r) {

return $decrypted;

}

return null;

}

/**

* @function 公钥加密

* @param $data

* @return string|null

*/

public function pubEncrypt ($data)

{

if (!is_string($data)) {

return null;

}

$r = openssl_public_encrypt($data, $encrypted, $this->_pubKey);

if ($r) {

return base64_encode($encrypted);

}

return null;

}

/**

* @function 公钥解密

* @param $data

* @return string|null

*/

public function pubDecrypt ($crypted)

{

if (!is_string($crypted)) {

return null;

}

$crypted = base64_decode($crypted);

$r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey);

if ($r) {

return $decrypted;

}

return null;

}

/**

* @function 私钥加签

* @param $data

* @return string|null

*/

public function sign ($data)

{

if (!is_string($data)) {

return null;

}

openssl_sign($data, $sign, $this->_privKey);

//base64编码

$sign = base64_encode($sign);

return $sign;

}

/**

* @function 公钥验签

* @param $data

* @return string|null

*/

public function verify($data, $sign){

if (!is_string($data)) {

return null;

}

$result = (bool)openssl_verify($data, base64_decode($sign), $this->_pubKey);

return $result;

}

public function __destruct ()

{

empty($this->_privKey) ? '' : openssl_free_key($this->_privKey);

empty($this->_pubKey) ? '' : openssl_free_key($this->_pubKey);

}

}

使用例子:

class Index

{

public function index()

{

$RSA = new Rsa(config('key_path'));

//对数据公钥加密及私钥解密

$string = '快乐程序员';

$pubString = $RSA->pubEncrypt($string);

echo '用公钥加密后数据:'.$pubString .'
';

$priDeString = $RSA->privDecrypt($pubString);

echo '用私钥解密数据:'.$priDeString .'
';

//实现对数据私钥加签及公钥验签

$sign = $RSA->sign($string);

echo '用私钥加签后得到签名:'.$sign .'
';

$result = $RSA->verify($string,$sign);

echo '验证签名是否正确:
';

dump($result);

}

}

得到结果:

以上内容希望帮助到大家,很多PHPer在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,对此我整理了一些资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx等多个知识点高级进阶干货需要的可以免费分享给大家,需要PHP进阶架构师>>>视频、面试文档免费获取​shimo.im

或 者关注咱们下面的知乎专栏PHP架构师圈子​zhuanlan.zhihu.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值