引自:https://blog.csdn.net/zfh168/article/details/83028183
钉钉回调加密包 mcrypt 修改为 openssl
最近公司oa项目要对接钉钉,要使用到回调,线上环境是php7.2 ,使用的是官方提供的加密解密包,开发完后测试发现掉坑里了 mcrypt报错 一查官方文档说 7.2以后移除了,建议用openssl替代 ,网上搜索一大堆 没一个能够解决问题的,没办法只能自己修改加密方法了,经测试无问题,直接贴上修改后的加密解密代码:
官方提供的加密方法:
public function encrypt($text, $corpid)
{
try {
//获得16位随机字符串,填充到明文之前
$random = $this->getRandomStr();
$text = $random . pack("N", strlen($text)) . $text . $corpid;
// 网络字节序
$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));
//使用BASE64对加密后的字符串进行编码
return array(ErrorCode::$OK, base64_encode($encrypted));
} catch (Exception $e) {
print $e;
return array(ErrorCode::$EncryptAESError, null);
}
}
修改为:
public function encrypt($text, $corpid)
{
try {
$random = $this->getRandomStr();
$text = $random . pack("N", strlen($text)) . $text . $corpid;
$iv = substr($this->key, 0, 16);
//使用自定义的填充方式对明文进行补位填充
$pkc_encoder = new PKCS7Encoder;
$text = $pkc_encoder->encode($text);
$encrypted = openssl_encrypt($text, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv);
return array(ErrorCode::$OK, base64_encode($encrypted));
} catch (Exception $e) {
print $e;
return array(ErrorCode::$EncryptAESError, null);
}
}
官方提供的解密方法:
public function decrypt($encrypted, $corpid)
{
try {
$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) {
return array(ErrorCode::$DecryptAESError, null);
}
try {
//去除补位字符
$pkc_encoder = new PKCS7Encoder;
$result = $pkc_encoder->decode($decrypted);
//去除16位随机字符串,网络字节序和AppId
if (strlen($result) < 16)
return "";
$content = substr($result, 16, strlen($result));
$len_list = unpack("N", substr($content, 0, 4));
$xml_len = $len_list[1];
$xml_content = substr($content, 4, $xml_len);
$from_corpid = substr($content, $xml_len + 4);
} catch (Exception $e) {
print $e;
return array(ErrorCode::$DecryptAESError, null);
}
if ($from_corpid != $corpid)
return array(ErrorCode::$ValidateSuiteKeyError, null);
return array(0, $xml_content);
}
修改为
public function decrypt($encrypted, $corpid)
{
try {
$ciphertext_dec = base64_decode($encrypted);
$iv = substr($this->key, 0, 16);
$decrypted = openssl_decrypt($ciphertext_dec, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv);
} catch (Exception $e) {
return array(ErrorCode::$DecryptAESError, null);
}
try {
//去除补位字符
$pkc_encoder = new PKCS7Encoder;
$result = $pkc_encoder->decode($decrypted);
//去除16位随机字符串,网络字节序和AppId
if (strlen($result) < 16)
return "";
$content = substr($result, 16, strlen($result));
$len_list = unpack("N", substr($content, 0, 4));
$xml_len = $len_list[1];
$xml_content = substr($content, 4, $xml_len);
$from_corpid = substr($content, $xml_len + 4);
} catch (Exception $e) {
print $e;
return array(ErrorCode::$DecryptAESError, null);
}
if ($from_corpid != $corpid)
return array(ErrorCode::$ValidateSuiteKeyError, null);
return array(0, $xml_content);
}