@@ -4645,89 +4645,63 @@ function Prpcrypt($k)
46454645 $ this ->key = base64_decode ($ k . "= " );
46464646 }
46474647
4648- /**
4649- * 对明文进行加密
4650- * @param string $text 需要加密的明文
4651- * @return string 加密后的密文
4652- */
4653- public function encrypt ($ text , $ appid )
4654- {
4655-
4656- try {
4657- //获得16位随机字符串,填充到明文之前
4658- $ random = $ this ->getRandomStr ();//"aaaabbbbccccdddd";
4659- $ text = $ random . pack ("N " , strlen ($ text )) . $ text . $ appid ;
4660- // 网络字节序
4661- $ size = mcrypt_get_block_size (MCRYPT_RIJNDAEL_128 , MCRYPT_MODE_CBC );
4662- $ module = mcrypt_module_open (MCRYPT_RIJNDAEL_128 , '' , MCRYPT_MODE_CBC , '' );
4663- $ iv = substr ($ this ->key , 0 , 16 );
4664- //使用自定义的填充方式对明文进行补位填充
4665- $ pkc_encoder = new PKCS7Encoder ;
4666- $ text = $ pkc_encoder ->encode ($ text );
4667- mcrypt_generic_init ($ module , $ this ->key , $ iv );
4668- //加密
4669- $ encrypted = mcrypt_generic ($ module , $ text );
4670- mcrypt_generic_deinit ($ module );
4671- mcrypt_module_close ($ module );
4672-
4673- // print(base64_encode($encrypted));
4674- //使用BASE64对加密后的字符串进行编码
4675- return array (ErrorCode::$ OK , base64_encode ($ encrypted ));
4676- } catch (Exception $ e ) {
4677- //print $e;
4678- return array (ErrorCode::$ EncryptAESError , null );
4679- }
4648+ /**
4649+ * 对明文进行加密
4650+ * @param string $text 需要加密的明文
4651+ * @return string 加密后的密文
4652+ */
4653+ public function encrypt ($ text , $ appid ){
4654+ try {
4655+ //获得16位随机字符串,填充到明文之前
4656+ $ random = $ this ->getRandomStr ();
4657+ $ text = $ random . pack ("N " , strlen ($ text )) . $ text . $ appid ;
4658+ $ iv = substr ($ this ->key , 0 , 16 );
4659+ $ pkc_encoder = new PKCS7Encoder ;
4660+ $ text = $ pkc_encoder ->encode ($ text );
4661+ $ encrypted = openssl_encrypt ($ text ,'AES-256-CBC ' ,substr ($ this ->key , 0 , 32 ),OPENSSL_ZERO_PADDING ,$ iv );
4662+ return array (ErrorCode::$ OK , $ encrypted );
4663+ } catch (Exception $ e ) {
4664+ //print $e;
4665+ return array (ErrorCode::$ EncryptAESError , null );
46804666 }
4681-
4682- /**
4683- * 对密文进行解密
4684- * @param string $encrypted 需要解密的密文
4685- * @return string 解密得到的明文
4686- */
4687- public function decrypt ($ encrypted , $ appid )
4688- {
4689-
4690- try {
4691- //使用BASE64对需要解密的字符串进行解码
4692- $ ciphertext_dec = base64_decode ($ encrypted );
4693- $ module = mcrypt_module_open (MCRYPT_RIJNDAEL_128 , '' , MCRYPT_MODE_CBC , '' );
4694- $ iv = substr ($ this ->key , 0 , 16 );
4695- mcrypt_generic_init ($ module , $ this ->key , $ iv );
4696- //解密
4697- $ decrypted = mdecrypt_generic ($ module , $ ciphertext_dec );
4698- mcrypt_generic_deinit ($ module );
4699- mcrypt_module_close ($ module );
4700- } catch (Exception $ e ) {
4701- return array (ErrorCode::$ DecryptAESError , null );
4702- }
4703-
4704-
4705- try {
4706- //去除补位字符
4707- $ pkc_encoder = new PKCS7Encoder ;
4708- $ result = $ pkc_encoder ->decode ($ decrypted );
4709- //去除16位随机字符串,网络字节序和AppId
4710- if (strlen ($ result ) < 16 )
4711- return "" ;
4712- $ content = substr ($ result , 16 , strlen ($ result ));
4713- $ len_list = unpack ("N " , substr ($ content , 0 , 4 ));
4714- $ xml_len = $ len_list [1 ];
4715- $ xml_content = substr ($ content , 4 , $ xml_len );
4716- $ from_appid = substr ($ content , $ xml_len + 4 );
4717- if (!$ appid )
4718- $ appid = $ from_appid ;
4719- //如果传入的appid是空的,则认为是订阅号,使用数据中提取出来的appid
4720- } catch (Exception $ e ) {
4721- //print $e;
4722- return array (ErrorCode::$ IllegalBuffer , null );
4723- }
4724- if ($ from_appid != $ appid )
4725- return array (ErrorCode::$ ValidateAppidError , null );
4726- //不注释上边两行,避免传入appid是错误的情况
4727- return array (0 , $ xml_content , $ from_appid ); //增加appid,为了解决后面加密回复消息的时候没有appid的订阅号会无法回复
4728-
4667+ }
4668+ /**
4669+ * 对密文进行解密
4670+ * @param string $encrypted 需要解密的密文
4671+ * @return string 解密得到的明文
4672+ */
4673+ public function decrypt ($ encrypted , $ appid ){
4674+ try {
4675+ $ iv = substr ($ this ->key , 0 , 16 );
4676+ $ decrypted = openssl_decrypt ($ encrypted ,'AES-256-CBC ' ,substr ($ this ->key , 0 , 32 ),OPENSSL_ZERO_PADDING ,$ iv );
4677+ } catch (Exception $ e ) {
4678+ return array (ErrorCode::$ DecryptAESError , null );
47294679 }
4730-
4680+ try {
4681+ //去除补位字符
4682+ $ pkc_encoder = new PKCS7Encoder ;
4683+ $ result = $ pkc_encoder ->decode ($ decrypted );
4684+ //去除16位随机字符串,网络字节序和AppId
4685+ if (strlen ($ result ) < 16 )
4686+ return "" ;
4687+ $ content = substr ($ result , 16 , strlen ($ result ));
4688+ $ len_list = unpack ("N " , substr ($ content , 0 , 4 ));
4689+ $ xml_len = $ len_list [1 ];
4690+ $ xml_content = substr ($ content , 4 , $ xml_len );
4691+ $ from_appid = substr ($ content , $ xml_len + 4 );
4692+ if (!$ appid )
4693+ $ appid = $ from_appid ;
4694+ //如果传入的appid是空的,则认为是订阅号,使用数据中提取出来的appid
4695+ } catch (Exception $ e ) {
4696+ //print $e;
4697+ return array (ErrorCode::$ IllegalBuffer , null );
4698+ }
4699+ if ($ from_appid != $ appid )
4700+ return array (ErrorCode::$ ValidateAppidError , null );
4701+ //不注释上边两行,避免传入appid是错误的情况
4702+ return array (0 , $ xml_content , $ from_appid );
4703+ //增加appid,为了解决后面加密回复消息的时候没有appid的订阅号会无法回复
4704+ }
47314705
47324706 /**
47334707 * 随机生成16位字符串
0 commit comments