栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java微信小程序步数encryptedData和开放数据解密的实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java微信小程序步数encryptedData和开放数据解密的实现

前提:

三个参数,
1.sessionKey(拿openId的时候可以得到)
2.encryptedData(前端提供)
3.iv(前端提供)

一个类,一个方法。

1.类:

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;
  }
}

2.方法:

import java.io.UnsupportedEncodingException;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.spec.InvalidParameterSpecException;
import java.util.HashMap;

import javax.annotation.Resource;
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 org.apache.commons.codec.binary.base64;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;


import lombok.extern.slf4j.Slf4j;
@Slf4j
public class AesCbcUtil {

  static {
     //BouncyCastle是一个开源的加解密解决方案,主页在http://www.bouncycastle.org/
     Security.addProvider(new BouncyCastleProvider());
   }
 
   
   public static String decrypt(String data, String key, String iv, String encodingFormat,Integer type) throws Exception {
//     initialize();
   if(StringUtils.isEmpty(data)||StringUtils.isEmpty(key)||StringUtils.isEmpty(iv))
    throw new SkyParamNullException("小程序获取用户信息参数不能为空");
     //被加密的数据
     byte[] dataByte = base64.decodebase64(data);
     //加密秘钥
     byte[] keyByte = base64.decodebase64(key);
     //偏移量
     byte[] ivByte = base64.decodebase64(iv);
 
 
     try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
 
SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
 
AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
parameters.init(new IvParameterSpec(ivByte));
 
cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
 
byte[] resultByte = cipher.doFinal(dataByte);
if (null != resultByte && resultByte.length > 0) {
if (type==1){
   return new String(WxPKCS7Encoder.decode(resultByte));

   }else {
   return new String(resultByte, encodingFormat);

   }


}
return null;
     } catch (NoSuchAlgorithmException e) {
e.printStackTrace();
log.error("小程序解析出错1{}",e.getMessage());
     } catch (NoSuchPaddingException e) {
e.printStackTrace();
log.error("小程序解析出错2{}",e.getMessage());
     } catch (InvalidParameterSpecException e) {
e.printStackTrace();
log.error("小程序解析出错3{}",e.getMessage());
     } catch (InvalidKeyException e) {
e.printStackTrace();
log.error("小程序解析出错4{}",e.getMessage());
     } catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
log.error("小程序解析出错5{}",e.getMessage());
     } catch (IllegalBlockSizeException e) {
e.printStackTrace();
log.error("小程序解析出错6{}",e.getMessage());
     } catch (BadPaddingException e) {
e.printStackTrace();
log.error("小程序解析出错7{}",e.getMessage());
     }
  catch (UnsupportedEncodingException e) {
  e.printStackTrace();
  log.error("小程序解析出错8{}",e.getMessage());
  }
     return null;
   }
}

实现

@ApiOperation(value = "wx步数解密")
  @PostMapping(value = "/decode")
  public ResultModel questionList(@RequestBody WxSportParam param) throws Exception {
    HashMap map = wxXiaoChenXuUtil.getWxOpenId(//这个方法网上很多,没有就用binarywang的
     param.getCode()//前端提供的code
     ,sysProperties.getWxAppId()//appID
      ,sysProperties.getWxAppSecret());//secret
    String sessionKey = map.get("session_key").toString();
    String result = AesCbcUtil.decrypt(param.getData(), sessionKey,param.getIv(), "UTF-8",1);

    return ResultModel.success(result);
  }



出来的数据 :

{ “stepInfoList”: [
 {
“timestamp”: 1445866601,
“step”: 100
 },
 {
“timestamp”: 1445876601,
“step”: 120
 } ] }

tips:如果是解析用户信息的话一样的用法,解密decrypt中参数type传0。两者区别在于字节的decode方法不一样而已。

到此这篇关于java微信小程序步数encryptedData和开放数据解密的实现的文章就介绍到这了,更多相关java微信小程序步数encryptedData内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号