php 公私钥,PHP 实现 RSA 公私钥加密
发布日期:2021-06-24 14:52:24 浏览次数:2 分类:技术文章

本文共 4314 字,大约阅读时间需要 14 分钟。

一个简易的 RSA 公私钥生成、签名、验签、加密、解密的类。<?php

/**

* RSA

*

* @version     1.0.0

* @author      Wildlife 

* @link        https://lanseyujie.com

* @copyright   Copyright(c) 2015-2019, lanseyujie.com

*/

class Rsa

{

public $path = './';

public $keyBits = 1024;

public $publicKey = null;

public $privateKey = null;

public function __construct()

{

if (file_exists($this->path . 'public.key') && file_exists($this->path . 'private.key')) {

$this->publicKey = file_get_contents($this->path . 'public.key');

$this->privateKey = file_get_contents($this->path . 'private.key');

} else if ($this->createKey()) {

$this->publicKey = file_get_contents($this->path . 'public.key');

$this->privateKey = file_get_contents($this->path . 'private.key');

} else {

exit;

}

}

/**

* Create Key

*

* @return bool

*/

public function createKey()

{

$path = $this->path;

$bits = $this->keyBits;

$config = array(

'private_key_bits' => (int)$bits,

);

$res = openssl_pkey_new($config);

openssl_pkey_export($res, $privateKey);

$publicKey = openssl_pkey_get_details($res);

$publicKey = $publicKey['key'];

if (file_exists($path)) {

file_put_contents($path . 'private.key', $privateKey);

file_put_contents($path . 'public.key', $publicKey);

return true;

}

return false;

}

/**

* Signature

*

* @param string $plainText

* @return string

*/

public function sign($plainText)

{

$privateKey = $this->privateKey;

$key = openssl_get_privatekey($privateKey);

openssl_sign($plainText, $sign, $key);

$sign = base64_encode($sign);

openssl_free_key($key);

return $sign;

}

/**

* Verify Signature

*

* @param string $plainText

* @param string $sign

* @return string

*/

public function verify($plainText, $sign)

{

$publicKey = $this->publicKey;

$key = openssl_get_publickey($publicKey);

$sign = base64_decode($sign);

$result = (bool)openssl_verify($plainText, $sign, $key);

openssl_free_key($key);

return $result;

}

#############################################################################################

/**

* Encrypt Plain Text By Public Key

*

* @param string $plainText

* @return string

*/

public function encryptByPublicKey($plainText)

{

$publicKey = $this->publicKey;

$key = openssl_get_publickey($publicKey);

$result  = '';

for ($i = 0; $i 

$data = substr($plainText, $i * 128, 128);

openssl_public_encrypt($data, $encrypt, $key);

$result .= $encrypt;

}

$result = base64_encode($result);

openssl_free_key($key);

return $result;

}

/**

* Decrypt Cipher Text By Private key

*

* @param string $cipherText

* @return string

*/

public function decryptByPrivateKey($cipherText)

{

$privateKey = $this->privateKey;

$key = openssl_get_privatekey($privateKey);

$cipherText = base64_decode($cipherText);

$result  = '';

for ($i = 0; $i 

$data = substr($cipherText, $i * 128, 128);

openssl_private_decrypt($data, $decrypt, $key);

$result .= $decrypt;

}

openssl_free_key($key);

return $result;

}

#############################################################################################

/**

* Encrypt Plain Text By Private key

*

* @param string $plainText

* @param string $privateKey

* @return string

*/

public function encryptByPrivateKey($plainText)

{

$privateKey = $this->privateKey;

$key = openssl_get_privatekey($privateKey);

$result  = '';

for ($i = 0; $i 

$data = substr($plainText, $i * 128, 128);

openssl_private_encrypt($data, $encrypt, $key);

$result .= $encrypt;

}

$result = base64_encode($result);

openssl_free_key($key);

return $result;

}

/**

* Decrypt Cipher Text By Public Key

*

* @param string $cipherText

* @param string $privateKey

* @return string

*/

public function decryptByPublicKey($cipherText)

{

$publicKey = $this->publicKey;

$key = openssl_get_publickey($publicKey);

$cipherText = base64_decode($cipherText);

$result  = '';

for ($i = 0; $i 

$data = substr($cipherText, $i * 128, 128);

openssl_public_decrypt($data, $decrypt, $key);

$result .= $decrypt;

}

openssl_free_key($key);

return $result;

}

}

测试require 'rsa.class.php';

$rsa = new Rsa();

$text = 'Test Sign';

echo $a = $rsa->sign($text) . "\n";

var_dump($rsa->verify($text, $a)) . "\n";

echo $b = $rsa->encryptByPublicKey('Hello World') . "\n";

echo $rsa->decryptByPrivateKey($b) . "\n";

echo $c = $rsa->encryptByPrivateKey('Hello') . "\n";

echo $rsa->decryptByPublicKey($c) . "\n";

d7cb968b5e11d55f3968ef6c9e994f29.png

OpenSSL 生成公私钥mkdir key && cd key

sudo apt install openssl

openssl

genrsa -out private.pem 1024

rsa -in private.pem -pubout -out public.pem

exit

81c09ece23309ee32470a855669ac440.png

转载地址:https://blog.csdn.net/weixin_33458486/article/details/115169575 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:php 获取文件创建时间,php获取文件的创建时间、修改时间的简单示例
下一篇:php 数组数值排序,按数值对PHP数组进行排序

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月18日 01时37分13秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章