11package cn .binarywang .wx .miniapp .util .crypt ;
22
3- import cn .binarywang .wx .miniapp .config .WxMaConfig ;
4- import me .chanjar .weixin .common .util .crypto .PKCS7Encoder ;
5- import org .apache .commons .codec .binary .Base64 ;
6-
3+ import java .nio .charset .Charset ;
4+ import java .nio .charset .StandardCharsets ;
5+ import java .security .AlgorithmParameters ;
6+ import java .security .Key ;
7+ import java .security .Security ;
8+ import java .util .Arrays ;
79import javax .crypto .Cipher ;
810import javax .crypto .spec .IvParameterSpec ;
911import javax .crypto .spec .SecretKeySpec ;
10- import java .nio .charset .StandardCharsets ;
11- import java .security .AlgorithmParameters ;
12+
13+ import org .apache .commons .codec .binary .Base64 ;
14+ import org .bouncycastle .jce .provider .BouncyCastleProvider ;
15+
16+ import cn .binarywang .wx .miniapp .config .WxMaConfig ;
17+ import me .chanjar .weixin .common .util .crypto .PKCS7Encoder ;
1218
1319/**
1420 * @author <a href="https://github.com/binarywang">Binary Wang</a>
1521 */
1622public class WxMaCryptUtils extends me .chanjar .weixin .common .util .crypto .WxCryptUtil {
23+ private static final Charset UTF_8 = StandardCharsets .UTF_8 ;
24+
1725 public WxMaCryptUtils (WxMaConfig config ) {
1826 this .appidOrCorpid = config .getAppid ();
1927 this .token = config .getToken ();
2028 this .aesKey = Base64 .decodeBase64 (config .getAesKey () + "=" );
2129 }
2230
2331 /**
24- * AES解密
32+ * AES解密.
2533 *
34+ * @param sessionKey session_key
2635 * @param encryptedData 消息密文
2736 * @param ivStr iv字符串
2837 */
@@ -34,9 +43,40 @@ public static String decrypt(String sessionKey, String encryptedData, String ivS
3443 Cipher cipher = Cipher .getInstance ("AES/CBC/NoPadding" );
3544 cipher .init (Cipher .DECRYPT_MODE , new SecretKeySpec (Base64 .decodeBase64 (sessionKey ), "AES" ), params );
3645
37- return new String (PKCS7Encoder .decode (cipher .doFinal (Base64 .decodeBase64 (encryptedData ))), StandardCharsets .UTF_8 );
46+ return new String (PKCS7Encoder .decode (cipher .doFinal (Base64 .decodeBase64 (encryptedData ))), UTF_8 );
47+ } catch (Exception e ) {
48+ throw new RuntimeException ("AES解密失败!" , e );
49+ }
50+ }
51+
52+
53+ /**
54+ * AES解密.
55+ *
56+ * @param sessionKey session_key
57+ * @param encryptedData 消息密文
58+ * @param ivStr iv字符串
59+ */
60+ public static String decryptAnotherWay (String sessionKey , String encryptedData , String ivStr ) {
61+ byte [] keyBytes = Base64 .decodeBase64 (sessionKey .getBytes (UTF_8 ));
62+
63+ int base = 16 ;
64+ if (keyBytes .length % base != 0 ) {
65+ int groups = keyBytes .length / base + (keyBytes .length % base != 0 ? 1 : 0 );
66+ byte [] temp = new byte [groups * base ];
67+ Arrays .fill (temp , (byte ) 0 );
68+ System .arraycopy (keyBytes , 0 , temp , 0 , keyBytes .length );
69+ keyBytes = temp ;
70+ }
71+
72+ Security .addProvider (new BouncyCastleProvider ());
73+ Key key = new SecretKeySpec (keyBytes , "AES" );
74+ try {
75+ Cipher cipher = Cipher .getInstance ("AES/CBC/PKCS7Padding" , "BC" );
76+ cipher .init (Cipher .DECRYPT_MODE , key , new IvParameterSpec (Base64 .decodeBase64 (ivStr .getBytes (UTF_8 ))));
77+ return new String (cipher .doFinal (Base64 .decodeBase64 (encryptedData .getBytes (UTF_8 ))), UTF_8 );
3878 } catch (Exception e ) {
39- throw new RuntimeException ("AES解密失败" , e );
79+ throw new RuntimeException ("AES解密失败! " , e );
4080 }
4181 }
4282
0 commit comments