Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mcrypt_module_open() 有替代方案可用吗? #22

Open
xiangzidetiandi opened this issue Mar 14, 2018 · 5 comments
Open

mcrypt_module_open() 有替代方案可用吗? #22

xiangzidetiandi opened this issue Mar 14, 2018 · 5 comments

Comments

@xiangzidetiandi
Copy link

mcrypt_module_open() 有替代方案可用吗?

@fisher-89
Copy link

要用openssl代替mcrypt
$encrypted = openssl_encrypt($text, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);

@LTaooo
Copy link

LTaooo commented Jan 8, 2020

要用openssl代替mcrypt
$encrypted = openssl_encrypt($text, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);

非常感谢了

@sunweij
Copy link

sunweij commented Apr 11, 2020

php7.2.1-nts(windows) Prpcrypt类中的构造函数写法是一个坑,难道就我发现吗,改成__construct就行了,也不用替换任何函数

@ByteByteDance
Copy link

php7.2.1-nts(windows) Prpcrypt类中的构造函数写法是一个坑,难道就我发现吗,改成__construct就行了,也不用替换任何函数

是因为这个sdk太老了

@miaowangjian
Copy link

pkcs7Encoder.php 文件,解密解密两个函数替换为:

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

		//print(base64_encode($encrypted));
		//使用BASE64对加密后的字符串进行编码
		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);
		$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::$IllegalBuffer, null);
	}
	if ($from_corpid != $corpid)
		return array(ErrorCode::$ValidateCorpidError, null);
	return array(0, $xml_content);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants