@@ -55,20 +55,19 @@ public WxCryptUtil() {
55
55
}
56
56
57
57
/**
58
- * 构造函数
58
+ * 构造函数.
59
59
*
60
60
* @param token 公众平台上,开发者设置的token
61
61
* @param encodingAesKey 公众平台上,开发者设置的EncodingAESKey
62
62
* @param appidOrCorpid 公众平台appid/corpid
63
63
*/
64
- public WxCryptUtil (String token , String encodingAesKey ,
65
- String appidOrCorpid ) {
64
+ public WxCryptUtil (String token , String encodingAesKey , String appidOrCorpid ) {
66
65
this .token = token ;
67
66
this .appidOrCorpid = appidOrCorpid ;
68
67
this .aesKey = Base64 .decodeBase64 (encodingAesKey + "=" );
69
68
}
70
69
71
- static String extractEncryptPart (String xml ) {
70
+ private static String extractEncryptPart (String xml ) {
72
71
try {
73
72
DocumentBuilder db = BUILDER_LOCAL .get ();
74
73
Document document = db .parse (new InputSource (new StringReader (xml )));
@@ -81,7 +80,7 @@ static String extractEncryptPart(String xml) {
81
80
}
82
81
83
82
/**
84
- * 将一个数字转换成生成4个字节的网络字节序bytes数组
83
+ * 将一个数字转换成生成4个字节的网络字节序bytes数组.
85
84
*/
86
85
private static byte [] number2BytesInNetworkOrder (int number ) {
87
86
byte [] orderBytes = new byte [4 ];
@@ -93,7 +92,7 @@ private static byte[] number2BytesInNetworkOrder(int number) {
93
92
}
94
93
95
94
/**
96
- * 4个字节的网络字节序bytes数组还原成一个数字
95
+ * 4个字节的网络字节序bytes数组还原成一个数字.
97
96
*/
98
97
private static int bytesNetworkOrder2Number (byte [] bytesInNetworkOrder ) {
99
98
int sourceNumber = 0 ;
@@ -105,7 +104,7 @@ private static int bytesNetworkOrder2Number(byte[] bytesInNetworkOrder) {
105
104
}
106
105
107
106
/**
108
- * 随机生成16位字符串
107
+ * 随机生成16位字符串.
109
108
*/
110
109
private static String genRandomStr () {
111
110
String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ;
@@ -119,16 +118,15 @@ private static String genRandomStr() {
119
118
}
120
119
121
120
/**
122
- * 生成xml消息
121
+ * 生成xml消息.
123
122
*
124
123
* @param encrypt 加密后的消息密文
125
124
* @param signature 安全签名
126
125
* @param timestamp 时间戳
127
126
* @param nonce 随机字符串
128
127
* @return 生成的xml字符串
129
128
*/
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 ) {
132
130
String format = "<xml>\n " + "<Encrypt><![CDATA[%1$s]]></Encrypt>\n "
133
131
+ "<MsgSignature><![CDATA[%2$s]]></MsgSignature>\n "
134
132
+ "<TimeStamp>%3$s</TimeStamp>\n " + "<Nonce><![CDATA[%4$s]]></Nonce>\n "
@@ -242,10 +240,9 @@ public String decrypt(String cipherText) {
242
240
try {
243
241
// 设置解密模式为AES的CBC模式
244
242
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 );
249
246
250
247
// 使用BASE64对密文进行解码
251
248
byte [] encrypted = Base64 .decodeBase64 (cipherText );
@@ -256,7 +253,8 @@ public String decrypt(String cipherText) {
256
253
throw new RuntimeException (e );
257
254
}
258
255
259
- String xmlContent , fromAppid ;
256
+ String xmlContent ;
257
+ String fromAppid ;
260
258
try {
261
259
// 去除补位字符
262
260
byte [] bytes = PKCS7Encoder .decode (original );
@@ -266,17 +264,15 @@ public String decrypt(String cipherText) {
266
264
267
265
int xmlLength = bytesNetworkOrder2Number (networkOrder );
268
266
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 );
273
269
} catch (Exception e ) {
274
270
throw new RuntimeException (e );
275
271
}
276
272
277
273
// appid不相同的情况
278
274
if (!fromAppid .equals (this .appidOrCorpid )) {
279
- throw new RuntimeException ("AppID不正确" );
275
+ throw new RuntimeException ("AppID不正确,请核实! " );
280
276
}
281
277
282
278
return xmlContent ;
0 commit comments