YOUDU发送消息MCRYPT到OPENSSL的AES加密转换

关键知识点,用key的长度位数来决定使用AES的编码位数:

AES-128-CBC = mcrpyt(cipher MCRYPT_RIJNDAEL_128 + key 16位 + iv 16 位 + mode MCRYPT_MODE_CBC)
AES-192-CBC = mcrpyt(cipher MCRYPT_RIJNDAEL_128 + key 24位 + iv 16 位 + mode MCRYPT_MODE_CBC)
AES-256-CBC = mcrpyt(cipher MCRYPT_RIJNDAEL_128 + key 32位 + iv 16 位 + mode MCRYPT_MODE_CBC)

YOUDU PHP旧SDK里面为PHP7.1的实现,其中用的是MCRYPT的AES加密:

public function encrypt($text, $appId)
{

	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);

		//使用BASE64对加密后的字符串进行编码
		return array(ErrorCode::$OK, base64_encode($encrypted));
	} catch (Exception $e) {
		print $e;
		return array(ErrorCode::$EncryptAESError, null);
	}
}

public function decrypt($encrypted, $appId)
{

	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);
	} 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));
		$json_len = $len_list[1];
		$json_content = substr($content, 4, $json_len);
		$from_appId = substr($content, $json_len + 4);
	} catch (Exception $e) {
		print $e;
		return array(ErrorCode::$IllegalBuffer, null);
	}
	if ($from_appId != $appId)
		return array(ErrorCode::$ValidateAppIdError, null);
	return array(0, $json_content);

}

 

在PHP7.2版本之后MCRYPT已经废弃,有OPENSSL类库实现AES加解密:

public function encrypt($text, $appId)
{
	try {
		
		
		//获得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', $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);

		//使用BASE64对加密后的字符串进行编码
		return array(ErrorCode::$OK, base64_encode($encrypted));
	} catch (Exception $e) {
		return array(ErrorCode::$EncryptAESError, null);
	}
}

public function decrypt($encrypted, $appId)
{

	try {
		//使用BASE64对需要解密的字符串进行解码
		$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));
		$json_len = $len_list[1];
		$json_content = substr($content, 4, $json_len);
		$from_appId = substr($content, $json_len + 4);
	} catch (Exception $e) {
		print $e;
		return array(ErrorCode::$IllegalBuffer, null);
	}
	if ($from_appId != $appId)
		return array(ErrorCode::$ValidateAppIdError, null);
	return array(0, $json_content);

}

 完成上面的转换即可完成有度无缝开发。

posted @ 2023-02-18 16:13  sblack  阅读(22)  评论(0编辑  收藏  举报