PHP的RSA工具类

前言

目前常见的算法加密有对称加密和非对称加密以及哈希算法。

在日常开发工程中,基本上与第三方对接,大多用RSA非对称加密方式,通过公钥私钥加密解密方式来进行数据的传输。

在PHP中使用RSA加密,需要安装openssl扩展

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php

/**
* RSA工具类
*/
class RSA
{
/**
* 签名算法
*/
const KEY_ALGORITHM = OPENSSL_KEYTYPE_RSA;
const SIGNATURE_ALGORITHM = OPENSSL_ALGO_MD5;
const EN_DE_ALGORITHM = OPENSSL_PKCS1_PADDING;

/**
* 字节数
*/
const PRIVATE_KEY_BITS = 1024;

const PUBLIC_TYPE = 'pub';
const PRIVATE_TYPE = 'pri';


/**
* 私钥加签
* @param string $dataStr 数据字符串
* @param string $privateKey 私钥
* @return string
*/
public static function sign($dataStr, $privateKey)
{
$dataStr = self::str2utf8($dataStr);
$privateKeyId = openssl_get_privatekey($privateKey);
openssl_sign($dataStr, $sign, $privateKeyId, self::SIGNATURE_ALGORITHM);
openssl_free_key($privateKeyId);
return base64_encode($sign);
}

/**
* 公钥验签
* @param string $dataStr 加签原数据字符串
* @param string $publicKey 公钥
* @param string $sign 签名
* @return bool
*/
public static function verifySign($dataStr, $publicKey, $sign)
{
$dataStr = self::str2utf8($dataStr);
$publicKeyId = openssl_get_publickey($publicKey);
return (boolean) openssl_verify($dataStr, base64_decode($sign), $publicKeyId, self::SIGNATURE_ALGORITHM);
}

/**
* 公钥加密
* @param string $dataStr 加签原数据字符串
* @param string $publicKey 公钥
* @return string
*/
public static function encryptByPublicKey($dataStr, $publicKey)
{
$dataStr = self::str2utf8($dataStr);
$publicKeyId = openssl_get_publickey($publicKey);
$data = "";

$dataArray = str_split($dataStr, self::PRIVATE_KEY_BITS / 8 - 11);
foreach ($dataArray as $value) {
openssl_public_encrypt($value,$encryptedTemp, $publicKeyId,self::EN_DE_ALGORITHM);
$data .= $encryptedTemp;
}
openssl_free_key($publicKeyId);
return base64_encode($data);
}

/**
* 私钥加密
* @param string $dataStr 加签原数据字符串
* @param string $privateKey 私钥
* @return string
*/
public static function encryptByPrivateKey($dataStr, $privateKey)
{
$dataStr = self::str2utf8($dataStr);
$privateKeyId = openssl_get_privatekey($privateKey);
$data = "";

$dataArray = str_split($dataStr, self::PRIVATE_KEY_BITS / 8 - 11);
foreach ($dataArray as $value) {
openssl_private_encrypt($value,$encryptedTemp, $privateKeyId,self::EN_DE_ALGORITHM);
$data .= $encryptedTemp;
}
openssl_free_key($privateKeyId);
return base64_encode($data);
}

/**
* 公钥解密
* @param string $encryptData 加密数据字符串
* @param string $publicKey 公钥
* @return string
*/
public static function decryptByPublicKey($encryptData, $publicKey) {
$decrypted = "";
$decodeStr = base64_decode($encryptData);
$publicKeyId = openssl_get_publickey($publicKey);

$enArray = str_split($decodeStr, self::PRIVATE_KEY_BITS / 8);

foreach ($enArray as $value) {
openssl_public_decrypt($value,$decryptedTemp, $publicKeyId,self::EN_DE_ALGORITHM);
$decrypted .= $decryptedTemp;
}
openssl_free_key($publicKeyId);
return $decrypted;
}

/**
* 私钥解密
* @param string $encryptData 加密数据字符串
* @param string $private 私钥
* @return string
*/
public static function decryptByPrivateKey($encryptData, $private) {
$decrypted = "";
$decodeStr = base64_decode($encryptData);
$privateKeyId = openssl_get_privatekey($private);

$enArray = str_split($decodeStr, self::PRIVATE_KEY_BITS / 8);

foreach ($enArray as $value) {
openssl_private_decrypt($value,$decryptedTemp, $privateKeyId,self::EN_DE_ALGORITHM);
$decrypted .= $decryptedTemp;
}
openssl_free_key($privateKeyId);
return $decrypted;
}

/**
* 公私钥转为字符串格式
* @param string $key 公私钥
* @return string
*/
public static function key2str($key)
{
$key = preg_replace('/-----.*-----/','', $key);
$key = preg_replace('/[\n\s]/','', $key);
return is_string($key) ? $key : '';
}


/**
* 将字符串编码转为 utf8
* @param $str
* @return string
*/
private static function str2utf8($str)
{
$encode = mb_detect_encoding($str, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
$str = $str ? $str : mb_convert_encoding($str, 'UTF-8', $encode);
$str = is_string($str) ? $str : '';
return $str;
}
}

使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php

use RSA;

class Debug{

const PRIVATE_KEY = "-----BEGIN RSA PRIVATE KEY-----
你的私钥内容
-----END RSA PRIVATE KEY-----";

//公钥
const PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----
你的公钥内容
-----END PUBLIC KEY-----";

public function test()
{
$dataStr = "测试";
$privateKey = self::PRIVATE_KEY;
$response = RSA::sign($dataStr,$privateKey);
var_dump($response,"++++++加密结果");


$publicKey = self::PUBLIC_KEY;
$result = RSA::decryptByPublicKey($response,$publicKey);
var_dump($result,"++++++解密结果");
}


}
-------------本文结束感谢您的阅读-------------
0%