之安全模式下消息加解密
在开发的时候,发现安全模式下,官⽅提供的demo中,加解密的⽅式还是⽤的Mcrypt扩展⽅式,但是在PHP7.1以上已经废弃了该⽅式,从⽽只能利⽤openssl来代替Mcrypt⽅式进⾏加解密
加密的Mcrypt扩展原代码为:
$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)
将其修改为openssl的代码为:
$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);
解密的Mcrypt扩展原代码为:
$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);php实例代码解密
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
将其修改为openssl的代码为:
$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);
同时tp5开发的时候,是命名空间引⼊,在使⽤DOMDocument类时,因它是全局命名空间中的⼀个类。要从本地名称空间中的全局名称空间引⽤类,必须使⽤完全限定名称或将其导⼊本地名称空间:如:$xml = new \DOMDocument();或者在开始就 use \DOMDocument;然后再$xml = new DOMDocument;两种⽅式都⾏
记录下,免得下次⼜忘记了!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。