Skip to content

Commit d6923f2

Browse files
committed
小程序解密工具类增加单元测试
1 parent 8311262 commit d6923f2

File tree

3 files changed

+89
-9
lines changed

3 files changed

+89
-9
lines changed

weixin-java-miniapp/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
<groupId>redis.clients</groupId>
7171
<artifactId>jedis</artifactId>
7272
</dependency>
73+
<dependency>
74+
<groupId>org.bouncycastle</groupId>
75+
<artifactId>bcpkix-jdk15on</artifactId>
76+
<version>1.59</version>
77+
</dependency>
7378
<dependency>
7479
<groupId>org.projectlombok</groupId>
7580
<artifactId>lombok</artifactId>

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/util/crypt/WxMaCryptUtils.java

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
package 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;
79
import javax.crypto.Cipher;
810
import javax.crypto.spec.IvParameterSpec;
911
import 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
*/
1622
public 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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cn.binarywang.wx.miniapp.util.crypt;
2+
3+
4+
import org.testng.annotations.*;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
/**
9+
* <pre>
10+
*
11+
* Created by Binary Wang on 2018/12/25.
12+
* </pre>
13+
*
14+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
15+
*/
16+
public class WxMaCryptUtilsTest {
17+
@Test
18+
public void testDecrypt() {
19+
String sessionKey = "7MG7jbTToVVRWRXVA885rg==";
20+
String encryptedData = "BY6VOgcWbwGcyrunK0ECWI8rnDsT69DucZ+M78tc1aL9aM/3bEAHFYd4fu7kRjWhD4YfjObw44T9vUqKyHIjbKs6hvtEasZZEIW35x4a91xVgN48ZqZ7MTQqUlP13kDUlkuwYh+/8g8yceu4kNbjowYrhihx+SV7CfjKCveJ7TSepr5Z7aLv1o+rfeelfOwn++WN/YoQsuZ6S3L4fWlWe5DAAUnFUI6cJvxxCohVzbrVXhyH2AqQdSjH2WnMYFeaGFIbcoxMznlk7oEwFn+hBj63dyT/swdYQfEdzuyCBmKXy8d6l1RKVX6Y65coTD8kIlbr+FKsqYrXVUIUBSwehqYuOdhYWZ9Bntl5DWU1oqzAPCnMn2cAIoQpQPKP7IGSxMOvCNAMhVXbE7BvnWuVuGF+AM5tXAa9IVUhcMImGwLQqm4iV5uBd+5OcFObh3A4VJk9iBCBWSkBHa/rV9CVoY0bFv2F9/2Hv82++Ybl274=";
21+
String ivStr = "TarMFjnzHVxy8pdS93wQbw==";
22+
System.out.println(WxMaCryptUtils.decrypt(sessionKey, encryptedData, ivStr));
23+
// System.out.println(WxMaCryptUtils.decryptAnotherWay(sessionKey, encryptedData, ivStr));
24+
}
25+
26+
@Test
27+
public void testDecryptAnotherWay() {
28+
String encryptedData = "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZMQmRzooG2xrDcvSnxIMXFufNstNGTyaGS9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+3hVbJSRgv+4lGOETKUQz6OYStslQ142dNCuabNPGBzlooOmB231qMM85d2/fV6ChevvXvQP8Hkue1poOFtnEtpyxVLW1zAo6/1Xx1COxFvrc2d7UL/lmHInNlxuacJXwu0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn/Hz7saL8xz+W//FRAUid1OksQaQx4CMs8LOddcQhULW4ucetDf96JcR3g0gfRK4PC7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns/8wR2SiRS7MNACwTyrGvt9ts8p12PKFdlqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYVoKlaRv85IfVunYzO0IKXsyl7JCUjCpoG20f0a04COwfneQAGGwd5oa+T8yO5hzuyDb/XcxxmK01EpqOyuxINew==";
29+
String ivStr = "r7BXXKkLb8qrSNn05n0qiA==";
30+
String sessionKey = "tiihtNczf5v6AKRyjwEUhQ==";
31+
32+
assertThat(WxMaCryptUtils.decrypt(sessionKey, encryptedData, ivStr))
33+
.isEqualTo(WxMaCryptUtils.decryptAnotherWay(sessionKey, encryptedData, ivStr));
34+
}
35+
}

0 commit comments

Comments
 (0)