|
@@ -64,7 +64,8 @@ class Prpcrypt
|
|
|
{
|
|
{
|
|
|
$this->key = base64_decode($k . "=");
|
|
$this->key = base64_decode($k . "=");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 对明文进行加密
|
|
* 对明文进行加密
|
|
|
* @param string $text 需要加密的明文
|
|
* @param string $text 需要加密的明文
|
|
@@ -72,33 +73,44 @@ class Prpcrypt
|
|
|
*/
|
|
*/
|
|
|
public function encrypt($text, $appid)
|
|
public function encrypt($text, $appid)
|
|
|
{
|
|
{
|
|
|
-
|
|
|
|
|
try {
|
|
try {
|
|
|
- //获得16位随机字符串,填充到明文之前
|
|
|
|
|
- $random = $this->getRandomStr();
|
|
|
|
|
- $text = $random . pack("N", strlen($text)) . $text . $appid;
|
|
|
|
|
- // 网络字节序
|
|
|
|
|
- $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
|
|
|
|
|
- $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
|
|
|
|
|
- $iv = substr($this->key, 0, 16);
|
|
|
|
|
- //使用自定义的填充方式对明文进行补位填充
|
|
|
|
|
- $pkc_encoder = new PKCS7Encoder;
|
|
|
|
|
- $text = $pkc_encoder->encode($text);
|
|
|
|
|
- mcrypt_generic_init($module, $this->key, $iv);
|
|
|
|
|
- //加密
|
|
|
|
|
- $encrypted = mcrypt_generic($module, $text);
|
|
|
|
|
- mcrypt_generic_deinit($module);
|
|
|
|
|
- mcrypt_module_close($module);
|
|
|
|
|
-
|
|
|
|
|
- //print(base64_encode($encrypted));
|
|
|
|
|
|
|
+ if(version_compare(PHP_VERSION, '7','>=')) {
|
|
|
|
|
+ //获得16位随机字符串,填充到明文之前
|
|
|
|
|
+ $random = $this->getRandomStr();
|
|
|
|
|
+ $text = $random . pack("N", strlen($text)) . $text . $appid;
|
|
|
|
|
+ $iv = substr($this->key, 0, 16);
|
|
|
|
|
+ $pkc_encoder = new PKCS7Encoder;
|
|
|
|
|
+ $text = $pkc_encoder->encode($text);
|
|
|
|
|
+ $encrypted = openssl_encrypt($text,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ //获得16位随机字符串,填充到明文之前
|
|
|
|
|
+ $random = $this->getRandomStr();
|
|
|
|
|
+ $text = $random . pack("N", strlen($text)) . $text . $appid;
|
|
|
|
|
+ // 网络字节序
|
|
|
|
|
+ $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
|
|
|
|
|
+ $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
|
|
|
|
|
+ $iv = substr($this->key, 0, 16);
|
|
|
|
|
+ //使用自定义的填充方式对明文进行补位填充
|
|
|
|
|
+ $pkc_encoder = new PKCS7Encoder;
|
|
|
|
|
+ $text = $pkc_encoder->encode($text);
|
|
|
|
|
+ mcrypt_generic_init($module, $this->key, $iv);
|
|
|
|
|
+ //加密
|
|
|
|
|
+ $encrypted = mcrypt_generic($module, $text);
|
|
|
|
|
+ mcrypt_generic_deinit($module);
|
|
|
|
|
+ mcrypt_module_close($module);
|
|
|
|
|
+ $encrypted = base64_encode($encrypted);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //print($encrypted);
|
|
|
//使用BASE64对加密后的字符串进行编码
|
|
//使用BASE64对加密后的字符串进行编码
|
|
|
- return array(ErrorCode::$OK, base64_encode($encrypted));
|
|
|
|
|
|
|
+ return array(ErrorCode::$OK, $encrypted);
|
|
|
} catch (Exception $e) {
|
|
} catch (Exception $e) {
|
|
|
//print $e;
|
|
//print $e;
|
|
|
return array(ErrorCode::$EncryptAESError, null);
|
|
return array(ErrorCode::$EncryptAESError, null);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 对密文进行解密
|
|
* 对密文进行解密
|
|
|
* @param string $encrypted 需要解密的密文
|
|
* @param string $encrypted 需要解密的密文
|
|
@@ -106,23 +118,29 @@ class Prpcrypt
|
|
|
*/
|
|
*/
|
|
|
public function decrypt($encrypted, $appid)
|
|
public function decrypt($encrypted, $appid)
|
|
|
{
|
|
{
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
- //使用BASE64对需要解密的字符串进行解码
|
|
|
|
|
- $ciphertext_dec = base64_decode($encrypted);
|
|
|
|
|
- $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
|
|
|
|
|
- $iv = substr($this->key, 0, 16);
|
|
|
|
|
- mcrypt_generic_init($module, $this->key, $iv);
|
|
|
|
|
-
|
|
|
|
|
- //解密
|
|
|
|
|
- $decrypted = mdecrypt_generic($module, $ciphertext_dec);
|
|
|
|
|
- mcrypt_generic_deinit($module);
|
|
|
|
|
- mcrypt_module_close($module);
|
|
|
|
|
|
|
+ if(version_compare(PHP_VERSION, '7','>=')) {
|
|
|
|
|
+ $iv = substr($this->key, 0, 16);
|
|
|
|
|
+ $decrypted = openssl_decrypt($encrypted,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ //使用BASE64对需要解密的字符串进行解码
|
|
|
|
|
+ $ciphertext_dec = base64_decode($encrypted);
|
|
|
|
|
+ $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
|
|
|
|
|
+ $iv = substr($this->key, 0, 16);
|
|
|
|
|
+ mcrypt_generic_init($module, $this->key, $iv);
|
|
|
|
|
+
|
|
|
|
|
+ //解密
|
|
|
|
|
+ $decrypted = mdecrypt_generic($module, $ciphertext_dec);
|
|
|
|
|
+ mcrypt_generic_deinit($module);
|
|
|
|
|
+ mcrypt_module_close($module);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
} catch (Exception $e) {
|
|
} catch (Exception $e) {
|
|
|
return array(ErrorCode::$DecryptAESError, null);
|
|
return array(ErrorCode::$DecryptAESError, null);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
//去除补位字符
|
|
//去除补位字符
|
|
|
$pkc_encoder = new PKCS7Encoder;
|
|
$pkc_encoder = new PKCS7Encoder;
|
|
@@ -135,14 +153,26 @@ class Prpcrypt
|
|
|
$xml_len = $len_list[1];
|
|
$xml_len = $len_list[1];
|
|
|
$xml_content = substr($content, 4, $xml_len);
|
|
$xml_content = substr($content, 4, $xml_len);
|
|
|
$from_appid = substr($content, $xml_len + 4);
|
|
$from_appid = substr($content, $xml_len + 4);
|
|
|
|
|
+ if(version_compare(PHP_VERSION, '7','>=')) {
|
|
|
|
|
+ if (!$appid) {
|
|
|
|
|
+ //如果传入的appid是空的,则认为是订阅号,使用数据中提取出来的appid
|
|
|
|
|
+ $appid = $from_appid;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
} catch (Exception $e) {
|
|
} catch (Exception $e) {
|
|
|
//print $e;
|
|
//print $e;
|
|
|
return array(ErrorCode::$IllegalBuffer, null);
|
|
return array(ErrorCode::$IllegalBuffer, null);
|
|
|
}
|
|
}
|
|
|
if ($from_appid != $appid)
|
|
if ($from_appid != $appid)
|
|
|
return array(ErrorCode::$ValidateAppidError, null);
|
|
return array(ErrorCode::$ValidateAppidError, null);
|
|
|
- return array(0, $xml_content);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ //不注释上边两行,避免传入appid是错误的情况
|
|
|
|
|
+ if(version_compare(PHP_VERSION, '7','>=')) {
|
|
|
|
|
+ //增加appid,为了解决后面加密回复消息的时候没有appid的订阅号会无法回复
|
|
|
|
|
+ return array(0, $xml_content, $from_appid);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return array(0, $xml_content);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|