@@ -35,16 +35,16 @@ public class WxCryptUtil {
3535 private static final Base64 base64 = new Base64 ();
3636 private static final Charset CHARSET = Charset .forName ("utf-8" );
3737
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+ };
4848
4949 protected byte [] aesKey ;
5050 protected String token ;
@@ -61,7 +61,8 @@ public WxCryptUtil() {
6161 * @param encodingAesKey 公众平台上,开发者设置的EncodingAESKey
6262 * @param appidOrCorpid 公众平台appid/corpid
6363 */
64- public WxCryptUtil (String token , String encodingAesKey , String appidOrCorpid ) {
64+ public WxCryptUtil (String token , String encodingAesKey ,
65+ String appidOrCorpid ) {
6566 this .token = token ;
6667 this .appidOrCorpid = appidOrCorpid ;
6768 this .aesKey = Base64 .decodeBase64 (encodingAesKey + "=" );
@@ -105,7 +106,8 @@ protected String encrypt(String randomStr, String plainText) {
105106 ByteGroup byteCollector = new ByteGroup ();
106107 byte [] randomStringBytes = randomStr .getBytes (CHARSET );
107108 byte [] plainTextBytes = plainText .getBytes (CHARSET );
108- byte [] bytesOfSizeInNetworkOrder = number2BytesInNetworkOrder (plainTextBytes .length );
109+ byte [] bytesOfSizeInNetworkOrder = number2BytesInNetworkOrder (
110+ plainTextBytes .length );
109111 byte [] appIdBytes = appidOrCorpid .getBytes (CHARSET );
110112
111113 // randomStr + networkBytesOrder + text + appid
@@ -154,7 +156,8 @@ protected String encrypt(String randomStr, String plainText) {
154156 * @param encryptedXml 密文,对应POST请求的数据
155157 * @return 解密后的原文
156158 */
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 ) {
158161 // 密钥,公众账号的app corpSecret
159162 // 提取密文
160163 String cipherText = extractEncryptPart (encryptedXml );
@@ -186,7 +189,8 @@ public String decrypt(String cipherText) {
186189 // 设置解密模式为AES的CBC模式
187190 Cipher cipher = Cipher .getInstance ("AES/CBC/NoPadding" );
188191 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 ));
190194 cipher .init (Cipher .DECRYPT_MODE , key_spec , iv );
191195
192196 // 使用BASE64对密文进行解码
@@ -208,9 +212,10 @@ public String decrypt(String cipherText) {
208212
209213 int xmlLength = bytesNetworkOrder2Number (networkOrder );
210214
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 ),
213216 CHARSET );
217+ from_appid = new String (
218+ Arrays .copyOfRange (bytes , 20 + xmlLength , bytes .length ), CHARSET );
214219 } catch (Exception e ) {
215220 throw new RuntimeException (e );
216221 }
@@ -224,34 +229,32 @@ public String decrypt(String cipherText) {
224229
225230 }
226231
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+ }
254253 }
254+ toSign .append ("key=" + signKey );
255+ String sign = DigestUtils .md5Hex (toSign .toString ()).toUpperCase ();
256+ return sign ;
257+ }
255258
256259 /**
257260 * 将一个数字转换成生成4个字节的网络字节序bytes数组
@@ -283,8 +286,6 @@ private int bytesNetworkOrder2Number(byte[] bytesInNetworkOrder) {
283286
284287 /**
285288 * 随机生成16位字符串
286- *
287- * @return
288289 */
289290 private String genRandomStr () {
290291 String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ;
@@ -306,14 +307,12 @@ private String genRandomStr() {
306307 * @param nonce 随机字符串
307308 * @return 生成的xml字符串
308309 */
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>" ;
317316 return String .format (format , encrypt , signature , timestamp , nonce );
318317 }
319318
0 commit comments