国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

java微信小程序步數encryptedData和開放數據解密的實現

瀏覽:165日期:2022-05-25 18:45:10

前提:

三個參數,1.sessionKey(拿openId的時候可以得到)2.encryptedData(前端提供)3.iv(前端提供)

一個類,一個方法。

1.類:

import java.nio.charset.Charset;import java.util.Arrays;/** * 微信小程序加解密 * @author liuyazhuang * */public class WxPKCS7Encoder { private static final Charset CHARSET = Charset.forName('utf-8'); private static final int BLOCK_SIZE = 32; /** * 獲得對明文進行補位填充的字節. * * @param count * 需要進行填充補位操作的明文字節個數 * @return 補齊用的字節數組 */ 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); } /** * 刪除解密后明文的補位字符 * * @param decrypted * 解密后的明文 * @return 刪除補位字符后的明文 */ 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); } /** * 將數字轉化成ASCII碼對應的字符,用于對明文進行補碼 * * @param a * 需要轉化的數字 * @return 轉化得到的字符 */ 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;@Slf4jpublic class AesCbcUtil { static { //BouncyCastle是一個開源的加解密解決方案,主頁在http://www.bouncycastle.org/ Security.addProvider(new BouncyCastleProvider()); } /** * AES解密 * * @param data //密文,被加密的數據 * @param key //秘鑰 * @param iv //偏移量 * @param encodingFormat //解密后的結果需要進行的編碼 * @param type //0 是其他 1是微信步數 * @return * @throws Exception */ 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<Object> questionList(@RequestBody WxSportParam param) throws Exception { HashMap<String, Object> 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內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: 微信
相關文章:
主站蜘蛛池模板: 99九九精品免费视频观看 | 中文字幕一级 | 91精品国产一区二区三区左线 | 午夜免费的国产片在线观看 | 免费视频男女 | 一个人看的www日本视频 | 男人天堂成人 | 精品久久成人免费第三区 | 亚洲大片| 久久久久久免费播放一级毛片 | 国产一区二区三区在线观看视频 | 午夜性刺激免费视频 | 亚洲成a人片在线看 | 久久三级网站 | 国产色爽女小说免费看 | 久久久不卡国产精品一区二区 | 韩国一级永久免费观看网址 | shkd在线观看 | 女人被男人躁得好爽免费视频免费 | 欧美日韩一级黄色片 | 国产一区二区久久久 | 中文字幕无线精品乱码一区 | 成视频年人黄网站免费 | 视频一区久久 | 久久性精品 | 香蕉久久国产 | 最新国产精品好看的国产精品 | 伊人久色 | 在线观看视频一区二区三区 | 日本三级香港三级人妇 m | 色综合久久久久 | 国产成人综合视频 | 一区二区三区国模大胆 | 国内精品线在线观看 | 国美女福利视频午夜精品 | 国产手机在线视频 | 在线中文字幕精品第5页 | 国内精品2020情侣视频 | 日韩三级在线 | 欧美在线看欧美高清视频免费 | 亚洲国产毛片aaaaa无费看 |