1
|
<?php
|
1
|
<?php
|
2
|
/**
|
2
|
/**
|
3
|
* AES, 128 ECB模式加密数据
|
3
|
* AES, 128 ECB模式加密数据
|
|
|
4
|
+ * 原有的填充方法没用到,否则与java、node不一致,暂时保留
|
4
|
*/
|
5
|
*/
|
5
|
|
6
|
|
6
|
namespace WebPlugin;
|
7
|
namespace WebPlugin;
|
|
@@ -10,20 +11,18 @@ class Encryption { |
|
@@ -10,20 +11,18 @@ class Encryption { |
10
|
//密钥
|
11
|
//密钥
|
11
|
private static $_secretKey = 'yoho9646abcdefgh';
|
12
|
private static $_secretKey = 'yoho9646abcdefgh';
|
12
|
|
13
|
|
13
|
- //前面补8位0
|
|
|
14
|
- private static $_preString = '00000000';
|
|
|
15
|
-
|
|
|
16
|
/**
|
14
|
/**
|
17
|
* 加密方法
|
15
|
* 加密方法
|
18
|
- * @param string $str
|
16
|
+ * @param string $str 加密字符
|
|
|
17
|
+ * @param string $key 密钥
|
19
|
* @return string
|
18
|
* @return string
|
20
|
*/
|
19
|
*/
|
21
|
- public static function encrypt($str){
|
|
|
22
|
- $str = self::$_preString.$str;
|
20
|
+ public static function encrypt($str, $key = ''){
|
|
|
21
|
+
|
23
|
//AES, 128 ECB模式加密数据
|
22
|
//AES, 128 ECB模式加密数据
|
24
|
- $secretKey = self::$_secretKey;
|
23
|
+ $secretKey = $key ? $key : self::$_secretKey;
|
25
|
$str = trim($str);
|
24
|
$str = trim($str);
|
26
|
- $str = self::addPKCS7Padding($str);
|
25
|
+// $str = self::addPKCS7Padding($str);
|
27
|
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB),MCRYPT_RAND);
|
26
|
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB),MCRYPT_RAND);
|
28
|
$encrypt_str = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $secretKey, $str, MCRYPT_MODE_ECB, $iv);
|
27
|
$encrypt_str = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $secretKey, $str, MCRYPT_MODE_ECB, $iv);
|
29
|
return base64_encode($encrypt_str);
|
28
|
return base64_encode($encrypt_str);
|
|
@@ -31,18 +30,18 @@ class Encryption { |
|
@@ -31,18 +30,18 @@ class Encryption { |
31
|
|
30
|
|
32
|
/**
|
31
|
/**
|
33
|
* 解密方法
|
32
|
* 解密方法
|
34
|
- * @param string $str
|
33
|
+ * @param string $str 解密字符
|
|
|
34
|
+ * @param string $key 密钥
|
35
|
* @return string
|
35
|
* @return string
|
36
|
*/
|
36
|
*/
|
37
|
- public static function decrypt($str){
|
37
|
+ public static function decrypt($str, $key = ''){
|
38
|
//AES, 128 ECB模式加密数据
|
38
|
//AES, 128 ECB模式加密数据
|
39
|
- $secretKey = self::$_secretKey;
|
39
|
+ $secretKey = $key ? $key : self::$_secretKey;
|
40
|
$str = base64_decode($str);
|
40
|
$str = base64_decode($str);
|
41
|
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB),MCRYPT_RAND);
|
41
|
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB),MCRYPT_RAND);
|
42
|
$encrypt_str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $secretKey, $str, MCRYPT_MODE_ECB, $iv);
|
42
|
$encrypt_str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $secretKey, $str, MCRYPT_MODE_ECB, $iv);
|
43
|
$encrypt_str = trim($encrypt_str);
|
43
|
$encrypt_str = trim($encrypt_str);
|
44
|
- $encrypt_str = self::stripPKSC7Padding($encrypt_str);
|
|
|
45
|
- $encrypt_str = ltrim($encrypt_str, self::$_preString);
|
44
|
+// $encrypt_str = self::stripPKSC7Padding($encrypt_str);
|
46
|
return $encrypt_str;
|
45
|
return $encrypt_str;
|
47
|
|
46
|
|
48
|
}
|
47
|
}
|