@@ -35,16 +35,16 @@ public class WxCryptUtil {
35
35
private static final Base64 base64 = new Base64 ();
36
36
private static final Charset CHARSET = Charset .forName ("utf-8" );
37
37
38
- private static final ThreadLocal <DocumentBuilder > builderLocal =
39
- new ThreadLocal < DocumentBuilder >() {
40
- @ Override protected DocumentBuilder initialValue () {
41
- try {
42
- return DocumentBuilderFactory .newInstance ().newDocumentBuilder ();
43
- } catch (ParserConfigurationException exc ) {
44
- throw new IllegalArgumentException (exc );
45
- }
46
- }
47
- };
38
+ private static final ThreadLocal <DocumentBuilder > builderLocal = new ThreadLocal < DocumentBuilder >() {
39
+ @ Override
40
+ protected DocumentBuilder initialValue () {
41
+ try {
42
+ return DocumentBuilderFactory .newInstance ().newDocumentBuilder ();
43
+ } catch (ParserConfigurationException exc ) {
44
+ throw new IllegalArgumentException (exc );
45
+ }
46
+ }
47
+ };
48
48
49
49
protected byte [] aesKey ;
50
50
protected String token ;
@@ -61,7 +61,8 @@ public WxCryptUtil() {
61
61
* @param encodingAesKey 公众平台上,开发者设置的EncodingAESKey
62
62
* @param appidOrCorpid 公众平台appid/corpid
63
63
*/
64
- public WxCryptUtil (String token , String encodingAesKey , String appidOrCorpid ) {
64
+ public WxCryptUtil (String token , String encodingAesKey ,
65
+ String appidOrCorpid ) {
65
66
this .token = token ;
66
67
this .appidOrCorpid = appidOrCorpid ;
67
68
this .aesKey = Base64 .decodeBase64 (encodingAesKey + "=" );
@@ -105,7 +106,8 @@ protected String encrypt(String randomStr, String plainText) {
105
106
ByteGroup byteCollector = new ByteGroup ();
106
107
byte [] randomStringBytes = randomStr .getBytes (CHARSET );
107
108
byte [] plainTextBytes = plainText .getBytes (CHARSET );
108
- byte [] bytesOfSizeInNetworkOrder = number2BytesInNetworkOrder (plainTextBytes .length );
109
+ byte [] bytesOfSizeInNetworkOrder = number2BytesInNetworkOrder (
110
+ plainTextBytes .length );
109
111
byte [] appIdBytes = appidOrCorpid .getBytes (CHARSET );
110
112
111
113
// randomStr + networkBytesOrder + text + appid
@@ -154,7 +156,8 @@ protected String encrypt(String randomStr, String plainText) {
154
156
* @param encryptedXml 密文,对应POST请求的数据
155
157
* @return 解密后的原文
156
158
*/
157
- public String decrypt (String msgSignature , String timeStamp , String nonce , String encryptedXml ) {
159
+ public String decrypt (String msgSignature , String timeStamp , String nonce ,
160
+ String encryptedXml ) {
158
161
// 密钥,公众账号的app corpSecret
159
162
// 提取密文
160
163
String cipherText = extractEncryptPart (encryptedXml );
@@ -186,7 +189,8 @@ public String decrypt(String cipherText) {
186
189
// 设置解密模式为AES的CBC模式
187
190
Cipher cipher = Cipher .getInstance ("AES/CBC/NoPadding" );
188
191
SecretKeySpec key_spec = new SecretKeySpec (aesKey , "AES" );
189
- IvParameterSpec iv = new IvParameterSpec (Arrays .copyOfRange (aesKey , 0 , 16 ));
192
+ IvParameterSpec iv = new IvParameterSpec (
193
+ Arrays .copyOfRange (aesKey , 0 , 16 ));
190
194
cipher .init (Cipher .DECRYPT_MODE , key_spec , iv );
191
195
192
196
// 使用BASE64对密文进行解码
@@ -208,9 +212,10 @@ public String decrypt(String cipherText) {
208
212
209
213
int xmlLength = bytesNetworkOrder2Number (networkOrder );
210
214
211
- xmlContent = new String (Arrays .copyOfRange (bytes , 20 , 20 + xmlLength ), CHARSET );
212
- from_appid = new String (Arrays .copyOfRange (bytes , 20 + xmlLength , bytes .length ),
215
+ xmlContent = new String (Arrays .copyOfRange (bytes , 20 , 20 + xmlLength ),
213
216
CHARSET );
217
+ from_appid = new String (
218
+ Arrays .copyOfRange (bytes , 20 + xmlLength , bytes .length ), CHARSET );
214
219
} catch (Exception e ) {
215
220
throw new RuntimeException (e );
216
221
}
@@ -224,34 +229,32 @@ public String decrypt(String cipherText) {
224
229
225
230
}
226
231
227
- /**
228
- * 微信公众号支付签名算法(详见:http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=4_3)
229
- * @param packageParams 原始参数
230
- * @param signKey 加密Key(即 商户Key)
231
- * @param charset 编码
232
- * @return 签名字符串
233
- */
234
- public static String createSign (Map <String , String > packageParams , String signKey ) {
235
- SortedMap <String , String > sortedMap = new TreeMap <String , String >();
236
- sortedMap .putAll (packageParams );
237
-
238
- List <String > keys = new ArrayList <String >(packageParams .keySet ());
239
- Collections .sort (keys );
240
-
241
-
242
- StringBuffer toSign = new StringBuffer ();
243
- for (String key : keys ) {
244
- String value = packageParams .get (key );
245
- if (null != value && !"" .equals (value ) && !"sign" .equals (key )
246
- && !"key" .equals (key )) {
247
- toSign .append (key + "=" + value + "&" );
248
- }
249
- }
250
- toSign .append ("key=" + signKey );
251
- String sign = DigestUtils .md5Hex (toSign .toString ())
252
- .toUpperCase ();
253
- return sign ;
232
+ /**
233
+ * 微信公众号支付签名算法(详见:http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=4_3)
234
+ * @param packageParams 原始参数
235
+ * @param signKey 加密Key(即 商户Key)
236
+ * @return 签名字符串
237
+ */
238
+ public static String createSign (Map <String , String > packageParams ,
239
+ String signKey ) {
240
+ SortedMap <String , String > sortedMap = new TreeMap <String , String >();
241
+ sortedMap .putAll (packageParams );
242
+
243
+ List <String > keys = new ArrayList <String >(packageParams .keySet ());
244
+ Collections .sort (keys );
245
+
246
+ StringBuffer toSign = new StringBuffer ();
247
+ for (String key : keys ) {
248
+ String value = packageParams .get (key );
249
+ if (null != value && !"" .equals (value ) && !"sign" .equals (key )
250
+ && !"key" .equals (key )) {
251
+ toSign .append (key + "=" + value + "&" );
252
+ }
254
253
}
254
+ toSign .append ("key=" + signKey );
255
+ String sign = DigestUtils .md5Hex (toSign .toString ()).toUpperCase ();
256
+ return sign ;
257
+ }
255
258
256
259
/**
257
260
* 将一个数字转换成生成4个字节的网络字节序bytes数组
@@ -283,8 +286,6 @@ private int bytesNetworkOrder2Number(byte[] bytesInNetworkOrder) {
283
286
284
287
/**
285
288
* 随机生成16位字符串
286
- *
287
- * @return
288
289
*/
289
290
private String genRandomStr () {
290
291
String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ;
@@ -306,14 +307,12 @@ private String genRandomStr() {
306
307
* @param nonce 随机字符串
307
308
* @return 生成的xml字符串
308
309
*/
309
- private String generateXml (String encrypt , String signature , String timestamp , String nonce ) {
310
- String format =
311
- "<xml>\n "
312
- + "<Encrypt><![CDATA[%1$s]]></Encrypt>\n "
313
- + "<MsgSignature><![CDATA[%2$s]]></MsgSignature>\n "
314
- + "<TimeStamp>%3$s</TimeStamp>\n "
315
- + "<Nonce><![CDATA[%4$s]]></Nonce>\n "
316
- + "</xml>" ;
310
+ private String generateXml (String encrypt , String signature , String timestamp ,
311
+ String nonce ) {
312
+ String format = "<xml>\n " + "<Encrypt><![CDATA[%1$s]]></Encrypt>\n "
313
+ + "<MsgSignature><![CDATA[%2$s]]></MsgSignature>\n "
314
+ + "<TimeStamp>%3$s</TimeStamp>\n " + "<Nonce><![CDATA[%4$s]]></Nonce>\n "
315
+ + "</xml>" ;
317
316
return String .format (format , encrypt , signature , timestamp , nonce );
318
317
}
319
318
0 commit comments