@@ -55,20 +55,19 @@ public WxCryptUtil() {
5555 }
5656
5757 /**
58- * 构造函数
58+ * 构造函数.
5959 *
6060 * @param token 公众平台上,开发者设置的token
6161 * @param encodingAesKey 公众平台上,开发者设置的EncodingAESKey
6262 * @param appidOrCorpid 公众平台appid/corpid
6363 */
64- public WxCryptUtil (String token , String encodingAesKey ,
65- String appidOrCorpid ) {
64+ public WxCryptUtil (String token , String encodingAesKey , String appidOrCorpid ) {
6665 this .token = token ;
6766 this .appidOrCorpid = appidOrCorpid ;
6867 this .aesKey = Base64 .decodeBase64 (encodingAesKey + "=" );
6968 }
7069
71- static String extractEncryptPart (String xml ) {
70+ private static String extractEncryptPart (String xml ) {
7271 try {
7372 DocumentBuilder db = BUILDER_LOCAL .get ();
7473 Document document = db .parse (new InputSource (new StringReader (xml )));
@@ -81,7 +80,7 @@ static String extractEncryptPart(String xml) {
8180 }
8281
8382 /**
84- * 将一个数字转换成生成4个字节的网络字节序bytes数组
83+ * 将一个数字转换成生成4个字节的网络字节序bytes数组.
8584 */
8685 private static byte [] number2BytesInNetworkOrder (int number ) {
8786 byte [] orderBytes = new byte [4 ];
@@ -93,7 +92,7 @@ private static byte[] number2BytesInNetworkOrder(int number) {
9392 }
9493
9594 /**
96- * 4个字节的网络字节序bytes数组还原成一个数字
95+ * 4个字节的网络字节序bytes数组还原成一个数字.
9796 */
9897 private static int bytesNetworkOrder2Number (byte [] bytesInNetworkOrder ) {
9998 int sourceNumber = 0 ;
@@ -105,7 +104,7 @@ private static int bytesNetworkOrder2Number(byte[] bytesInNetworkOrder) {
105104 }
106105
107106 /**
108- * 随机生成16位字符串
107+ * 随机生成16位字符串.
109108 */
110109 private static String genRandomStr () {
111110 String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ;
@@ -119,16 +118,15 @@ private static String genRandomStr() {
119118 }
120119
121120 /**
122- * 生成xml消息
121+ * 生成xml消息.
123122 *
124123 * @param encrypt 加密后的消息密文
125124 * @param signature 安全签名
126125 * @param timestamp 时间戳
127126 * @param nonce 随机字符串
128127 * @return 生成的xml字符串
129128 */
130- private static String generateXml (String encrypt , String signature ,
131- String timestamp , String nonce ) {
129+ private static String generateXml (String encrypt , String signature , String timestamp , String nonce ) {
132130 String format = "<xml>\n " + "<Encrypt><![CDATA[%1$s]]></Encrypt>\n "
133131 + "<MsgSignature><![CDATA[%2$s]]></MsgSignature>\n "
134132 + "<TimeStamp>%3$s</TimeStamp>\n " + "<Nonce><![CDATA[%4$s]]></Nonce>\n "
@@ -242,10 +240,9 @@ public String decrypt(String cipherText) {
242240 try {
243241 // 设置解密模式为AES的CBC模式
244242 Cipher cipher = Cipher .getInstance ("AES/CBC/NoPadding" );
245- SecretKeySpec key_spec = new SecretKeySpec (this .aesKey , "AES" );
246- IvParameterSpec iv = new IvParameterSpec (
247- Arrays .copyOfRange (this .aesKey , 0 , 16 ));
248- cipher .init (Cipher .DECRYPT_MODE , key_spec , iv );
243+ SecretKeySpec keySpec = new SecretKeySpec (this .aesKey , "AES" );
244+ IvParameterSpec iv = new IvParameterSpec (Arrays .copyOfRange (this .aesKey , 0 , 16 ));
245+ cipher .init (Cipher .DECRYPT_MODE , keySpec , iv );
249246
250247 // 使用BASE64对密文进行解码
251248 byte [] encrypted = Base64 .decodeBase64 (cipherText );
@@ -256,7 +253,8 @@ public String decrypt(String cipherText) {
256253 throw new RuntimeException (e );
257254 }
258255
259- String xmlContent , fromAppid ;
256+ String xmlContent ;
257+ String fromAppid ;
260258 try {
261259 // 去除补位字符
262260 byte [] bytes = PKCS7Encoder .decode (original );
@@ -266,17 +264,15 @@ public String decrypt(String cipherText) {
266264
267265 int xmlLength = bytesNetworkOrder2Number (networkOrder );
268266
269- xmlContent = new String (Arrays .copyOfRange (bytes , 20 , 20 + xmlLength ),
270- CHARSET );
271- fromAppid = new String (
272- Arrays .copyOfRange (bytes , 20 + xmlLength , bytes .length ), CHARSET );
267+ xmlContent = new String (Arrays .copyOfRange (bytes , 20 , 20 + xmlLength ), CHARSET );
268+ fromAppid = new String (Arrays .copyOfRange (bytes , 20 + xmlLength , bytes .length ), CHARSET );
273269 } catch (Exception e ) {
274270 throw new RuntimeException (e );
275271 }
276272
277273 // appid不相同的情况
278274 if (!fromAppid .equals (this .appidOrCorpid )) {
279- throw new RuntimeException ("AppID不正确" );
275+ throw new RuntimeException ("AppID不正确,请核实! " );
280276 }
281277
282278 return xmlContent ;
0 commit comments