Micro-channel third-party platform for solutions ticket error (PHP)

problem:
Micro-channel message encryption and decryption program to develop small, dome officially given normal use, recently upgraded to PHP 7.1 version of the project found that to receive messages can not be decrypted, and finally checked the log and check a variety of information it has been found Mcrypt function php7.1 waste; 
Solution:
Instead of using openssl Mcrypt in Prpcrypt class. code show as below:
Official to code:
/ * * 
* Ciphertext decryption 
* @param string $ encrypted to be decrypted ciphertext 
* @return string decrypted plaintext 
* / 
public  function the decrypt ( $ ENCRYPTED , $ AppID ) 
{ 

the try {
 // used to decrypt the need BASE64 string decoding 
$ ciphertext_dec = base64_decode ( $ ENCRYPTED );
 $ Module1 = mcrypt_module_open (MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '' );
 $ IV = substr ( $ the this -> Key , 0, 16 ); 
mcrypt_generic_init ( $ Module1 , $ the 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 );
 // removing the 16-bit random string, and network byte order AppId 
IF ( strlen ( $ Result ) <16 )
 return "" ;
 $ Content = substr ( $ Result , 16, strlen ( $ Result ));
 $ len_list = the unpack ( "N", substr ( $ Content , 0,. 4 ));
 $ xml_len = $ len_list [. 1 ];
 $ xml_content = substr ( $ Content ,. 4, $ xml_len);
$from_appid = substr($content, $xml_len + 4);
} catch (Exception $e) {
//print $e;
return array(ErrorCode::$IllegalBuffer, null);
}
if ($from_appid != $appid)
return array(ErrorCode::$ValidateAppidError, null);
return array(0, $xml_content);

}

 

After modifying the code:

/ * * 
* Ciphertext decryption 
* @param string $ encrypted to be decrypted ciphertext 
* @return string decrypted plaintext 
* / 
public  function the decrypt ( $ ENCRYPTED , $ AppID ) 
{ 

the try {
 $ IV = substr ( $ the this -> Key , 0, 16 );
 $ DECRYPTED = openssl_decrypt ( base64_decode ( $ ENCRYPTED ), 'the AES-the CBC-256', $ the this -> Key , OPENSSL_RAW_DATA, $ IV ); 
} the catch ( Exception  $ E ) {
return  Array (the ErrorCode :: $ DecryptAESError , null ); 
} 
the try {
 // remove fill characters 
$ pkc_encoder = new new PKCS7Encoder;
 $ Result = $ pkc_encoder -> decode ( $ DECRYPTED );
 // removing the 16-bit random string network endian and 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_appid = substr($content, $xml_len + 4);
} catch (Exception $e) {
//print $e;
return array(ErrorCode::$IllegalBuffer, null);
}
if ($from_appid != $appid)
return array(ErrorCode::$ValidateAppidError, null);
return array(0, $xml_content);

}

 

 

Guess you like

Origin www.cnblogs.com/yuanwanli/p/12616881.html