11package me .chanjar .weixin .common .util .crypto ;
22
3- import org . apache . commons . codec . binary . Base64 ;
4- import org . w3c . dom . Document ;
5- import org . w3c . dom . Element ;
6- import org . xml . sax . InputSource ;
7-
3+ import java . io . StringReader ;
4+ import java . nio . charset . Charset ;
5+ import java . nio . charset . StandardCharsets ;
6+ import java . util . Arrays ;
7+ import java . util . Random ;
88import javax .crypto .Cipher ;
99import javax .crypto .spec .IvParameterSpec ;
1010import javax .crypto .spec .SecretKeySpec ;
1111import javax .xml .parsers .DocumentBuilder ;
1212import javax .xml .parsers .DocumentBuilderFactory ;
1313import javax .xml .parsers .ParserConfigurationException ;
14- import java . io . StringReader ;
15- import java . nio . charset . Charset ;
16- import java . nio . charset . StandardCharsets ;
17- import java . util . Arrays ;
18- import java . util . Random ;
14+
15+ import org . apache . commons . codec . binary . Base64 ;
16+ import org . w3c . dom . Document ;
17+ import org . w3c . dom . Element ;
18+ import org . xml . sax . InputSource ;
1919
2020/**
2121 * <pre>
2525 * 需要导入架包commons-codec-1.9(或commons-codec-1.8等其他版本)
2626 * 官方下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi
2727 * </pre>
28+ *
29+ * @author Tencent
2830 */
2931public class WxCryptUtil {
3032
@@ -164,8 +166,7 @@ protected String encrypt(String randomStr, String plainText) {
164166 ByteGroup byteCollector = new ByteGroup ();
165167 byte [] randomStringBytes = randomStr .getBytes (CHARSET );
166168 byte [] plainTextBytes = plainText .getBytes (CHARSET );
167- byte [] bytesOfSizeInNetworkOrder = number2BytesInNetworkOrder (
168- plainTextBytes .length );
169+ byte [] bytesOfSizeInNetworkOrder = number2BytesInNetworkOrder (plainTextBytes .length );
169170 byte [] appIdBytes = this .appidOrCorpid .getBytes (CHARSET );
170171
171172 // randomStr + networkBytesOrder + text + appid
@@ -252,7 +253,7 @@ public String decrypt(String cipherText) {
252253 throw new RuntimeException (e );
253254 }
254255
255- String xmlContent , from_appid ;
256+ String xmlContent , fromAppid ;
256257 try {
257258 // 去除补位字符
258259 byte [] bytes = PKCS7Encoder .decode (original );
@@ -264,14 +265,14 @@ public String decrypt(String cipherText) {
264265
265266 xmlContent = new String (Arrays .copyOfRange (bytes , 20 , 20 + xmlLength ),
266267 CHARSET );
267- from_appid = new String (
268+ fromAppid = new String (
268269 Arrays .copyOfRange (bytes , 20 + xmlLength , bytes .length ), CHARSET );
269270 } catch (Exception e ) {
270271 throw new RuntimeException (e );
271272 }
272273
273274 // appid不相同的情况
274- if (!from_appid .equals (this .appidOrCorpid )) {
275+ if (!fromAppid .equals (this .appidOrCorpid )) {
275276 throw new RuntimeException ("AppID不正确" );
276277 }
277278
0 commit comments