Add WXBizDataCrypt for WeChat Mini Program.
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package com.foxinmy.weixin4j.wxa;
|
||||
|
||||
import java.security.AlgorithmParameters;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.Key;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.security.Security;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
|
||||
final class AESUtils {
|
||||
|
||||
private static boolean initialized = false;
|
||||
|
||||
private AESUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* AES解密
|
||||
*
|
||||
* @param content 密文
|
||||
* @return 明文
|
||||
* @throws InvalidAlgorithmParameterException
|
||||
* @throws NoSuchProviderException
|
||||
*/
|
||||
static byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte)
|
||||
throws InvalidAlgorithmParameterException {
|
||||
initialize();
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
|
||||
Key sKeySpec = new SecretKeySpec(keyByte, "AES");
|
||||
|
||||
cipher.init(Cipher.DECRYPT_MODE, sKeySpec, generateIV(ivByte)); // 初始化
|
||||
byte[] result = cipher.doFinal(content);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void initialize() {
|
||||
if (initialized)
|
||||
return;
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
private static AlgorithmParameters generateIV(byte[] iv) throws Exception {
|
||||
AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
|
||||
params.init(new IvParameterSpec(iv));
|
||||
return params;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.foxinmy.weixin4j.wxa;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
/**
|
||||
* @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api/signature.html#wxchecksessionobject">加密数据解密算法</a>
|
||||
*/
|
||||
public class WXBizDataCrypt {
|
||||
|
||||
private final String appid;
|
||||
|
||||
private final String sessionKey;
|
||||
|
||||
public WXBizDataCrypt(String appid, String sessionKey) {
|
||||
this.appid = appid;
|
||||
this.sessionKey = sessionKey;
|
||||
}
|
||||
|
||||
public JSONObject decryptData(final String encryptedData, final String iv) {
|
||||
final byte[] aesKey = Base64.decodeBase64(sessionKey);
|
||||
final byte[] aesCipher = Base64.decodeBase64(encryptedData);
|
||||
final byte[] aesIV = Base64.decodeBase64(iv);
|
||||
|
||||
final byte[] resultByte;
|
||||
try {
|
||||
resultByte = AESUtils.decrypt(aesCipher, aesKey, aesIV);
|
||||
} catch (InvalidAlgorithmParameterException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
final String decryptedText = new String(resultByte, Charset.forName("UTF-8"));
|
||||
final JSONObject decrypted = JSON.parseObject(decryptedText);
|
||||
|
||||
final String appId = decrypted.getJSONObject("watermark").getString("appid");
|
||||
if (!appId.equals(this.appid)) {
|
||||
throw new IllegalArgumentException("Invalid Buffer");
|
||||
}
|
||||
|
||||
return decrypted;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* <a href="https://developers.weixin.qq.com/miniprogram/dev/api/">WeChat Mini Program</a> support library.
|
||||
*/
|
||||
package com.foxinmy.weixin4j.wxa;
|
||||
Reference in New Issue
Block a user