Md5Rsa加密
Md5rsa.php
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
namespace libs;
class Md5rsa{
/**
* 利用约定数据和私钥生成数字签名
* @param $data 待签数据
* @return String 返回签名
*/
public function sign($data='')
{
if (empty($data))
{
return False;
}
$private_key = file_get_contents(dirname(__FILE__) . '/rsa_private_key.pem');
if (empty($private_key))
{
echo "Private Key error!";
return False;
}
$pkeyid = openssl_get_privatekey($private_key);
if (empty($pkeyid))
{
echo "private key resource identifier False!";
return False;
}
$verify = openssl_sign($data, $signature, $pkeyid, OPENSSL_ALGO_MD5);
openssl_free_key($pkeyid);
return $signature;
}
/**
* 利用公钥和数字签名以及约定数据验证合法性
* @param $data 待验证数据
* @param $signature 数字签名
* @return -1:error验证错误 1:correct验证成功 0:incorrect验证失败
*/
public function isValid($data='', $signature='')
{
if (empty($data) || empty($signature))
{
return False;
}
$public_key = file_get_contents(dirname(__FILE__) . '/rsa_java_public_key.pem');//rsa_public_key
if (empty($public_key))
{
echo "Public Key error!";
return False;
}
$pkeyid = openssl_get_publickey($public_key);
if (empty($pkeyid))
{
echo "public key resource identifier False!";
return False;
}
$ret = openssl_verify($data, $signature, $pkeyid, OPENSSL_ALGO_MD5);
switch ($ret)
{
case -1:
echo "error";
return false;
// break;
default:
if($ret==1){
$ret = 1;
}else{
return false;
}
// echo $ret==1 ? "true" : "false";//0:incorrect
break;
}
return $ret;
}
}加签名
1
2
3
4
5public function myMd5rsa($data){
$md5Rsa = new Md5rsa();
$res = $md5Rsa->sign($data);
return $res;
}验签名
1
2
3
4
5
6
7public function checkPass(){
$res = (new Md5rsa())
->isValid($post['data'],base64_decode($post['sign']));
if($res===false){
ajaxReturn(0,'验证失败');
}
}