本文主要是介绍原创:微信小程序java实现AES解密并获取unionId,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
如果大家使用小程序的同时还在使用公众号的话,可能会用到unionId这种功能,由于公司业务需要,我们需要使用unionId,具体使用方法,请参考微信开放平台的说明,但是在微信小程序的文档中只给出了部分语言实现的源码,竟然没有Java的,小程序的开发人员是有多么懒。难道大家都不用java写后台???
什么鬼,然后开始了各种AES踩坑之路,其实参考了很多的网上的教程,再次不能一一列出来给大家了,(因为我写这篇文章的时候,已经是解决问题一周以后了),也收到管理员的很多帮助,再次写个帖子回馈大家吧,在此只列出unionId的解密方式,如果有什么问题,联系我或者回帖都可以。
另外稍加吐槽一下,
https 不要用startcom提供的免费证书!
https 不要用startcom提供的免费证书!
https 不要用startcom提供的免费证书!
重要的事情说三遍!!!!
AES.java
- <span style="font-family:Microsoft YaHei;font-size:12px;">import org.apache.commons.codec.binary.Base64;
- import org.bouncycastle.jce.provider.BouncyCastleProvider;
- import javax.crypto.BadPaddingException;
- import javax.crypto.Cipher;
- import javax.crypto.IllegalBlockSizeException;
- import javax.crypto.NoSuchPaddingException;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- import java.security.*;
- public class AES {
- public static boolean initialized = false;
-
-
-
-
-
-
-
- public 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 (NoSuchAlgorithmException e) {
- e.printStackTrace();
- } catch (NoSuchPaddingException e) {
- e.printStackTrace();
- } catch (InvalidKeyException e) {
- e.printStackTrace();
- } catch (IllegalBlockSizeException e) {
- e.printStackTrace();
- } catch (BadPaddingException e) {
- e.printStackTrace();
- } catch (NoSuchProviderException e) {
-
- e.printStackTrace();
- } catch (Exception e) {
-
- e.printStackTrace();
- }
- return null;
- }
- public static void initialize(){
- if (initialized) return;
- Security.addProvider(new BouncyCastleProvider());
- initialized = true;
- }
-
- public static AlgorithmParameters generateIV(byte[] iv) throws Exception{
- AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
- params.init(new IvParameterSpec(iv));
- return params;
- }
- }</span>
WxPKCS7Encoder.java
- <span style="font-family:Microsoft YaHei;font-size:12px;">import java.nio.charset.Charset;
- import java.util.Arrays;
-
-
-
- public class WxPKCS7Encoder {
- private static final Charset CHARSET = Charset.forName("utf-8");
- private static final int BLOCK_SIZE = 32;
-
-
-
-
-
-
- public static byte[] encode(int count) {
-
- int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE);
- if (amountToPad == 0) {
- amountToPad = BLOCK_SIZE;
- }
-
- char padChr = chr(amountToPad);
- String tmp = new String();
- for (int index = 0; index < amountToPad; index++) {
- tmp += padChr;
- }
- return tmp.getBytes(CHARSET);
- }
-
-
-
-
-
-
- public static byte[] decode(byte[] decrypted) {
- int pad = decrypted[decrypted.length - 1];
- if (pad < 1 || pad > 32) {
- pad = 0;
- }
- return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad);
- }
-
-
-
-
-
-
- public static char chr(int a) {
- byte target = (byte) (a & 0xFF);
- return (char) target;
- }
- }</span>
调用方法解密如下:
- <span style="font-family:Microsoft YaHei;font-size:12px;">WechatOpenIdRes wechatInfo = getWehatInfoByCode(code);
- if(wechatInfo != null && wechatInfo.isOk()){
- boolean isNew = true;
- try {
- AES aes = new AES();
- byte[] resultByte = aes.decrypt(Base64.decodeBase64(encryptedData), Base64.decodeBase64(wechatInfo.getSession_key()), Base64.decodeBase64(iv));
- if(null != resultByte && resultByte.length > 0){
- String userInfo = new String(WxPKCS7Encoder.decode(resultByte) ,"UTF-8");
- WxInfo wxInfo = GsonUtil.fromGson(userInfo, WxInfo.class);
- if(wxInfo != null) {
- logger.debug("xxxxxunionid===="+wxInfo.getUnionId());
- }
- }
- } catch (InvalidAlgorithmParameterException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }</span>
编译环境为java1.8
另外我引入的support 包为
bcprov-jdk16-139.jar 此包已上传附件,
顺带附上我试用的小程序js中的代码吧,
- <span style="font-family:Microsoft YaHei;font-size:12px;">var code ="";
- wechat.login()
- .then(function(res){
- code = res.code;
- })
- .then(function(){
- return wechat.getUserInfo();
- })
- .then(function(res){
- var encryptedData = res.encryptedData
- var iv = res.iv;
- return userservice.getUserToken(code,encryptedData,iv);
- })</span>
上面的代码使用了promise,其中最后一句userservice.getUserToken为请求服务器的方法,参数为获取到的code+加密内容+初始化向量
有什么问题可以联系我。
qq:403125094
源码下载地址:http://www.wxapp-union.com/portal.php?mod=view&aid=1189
这篇关于原创:微信小程序java实现AES解密并获取unionId的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!