欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java微信小程序步數(shù)encryptedData和開放數(shù)據(jù)解密的實(shí)現(xiàn)

 更新時(shí)間:2020年09月27日 09:30:09   作者:通尼渣渣  
這篇文章主要介紹了java微信小程序步數(shù)encryptedData和開放數(shù)據(jù)解密的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前提:

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

一個(gè)類,一個(gè)方法。

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;
 
  /**
   * 獲得對明文進(jìn)行補(bǔ)位填充的字節(jié).
   *
   * @param count
   *      需要進(jìn)行填充補(bǔ)位操作的明文字節(jié)個(gè)數(shù)
   * @return 補(bǔ)齊用的字節(jié)數(shù)組
   */
  public static byte[] encode(int count) {
    // 計(jì)算需要填充的位數(shù)
    int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE);
    if (amountToPad == 0) {
      amountToPad = BLOCK_SIZE;
    }
    // 獲得補(bǔ)位所用的字符
    char padChr = chr(amountToPad);
    String tmp = new String();
    for (int index = 0; index < amountToPad; index++) {
      tmp += padChr;
    }
    return tmp.getBytes(CHARSET);
  }
 
  /**
   * 刪除解密后明文的補(bǔ)位字符
   *
   * @param decrypted
   *      解密后的明文
   * @return 刪除補(bǔ)位字符后的明文
   */
  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);
  }
 
  /**
   * 將數(shù)字轉(zhuǎn)化成ASCII碼對應(yīng)的字符,用于對明文進(jìn)行補(bǔ)碼
   *
   * @param a
   *      需要轉(zhuǎn)化的數(shù)字
   * @return 轉(zhuǎn)化得到的字符
   */
  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是一個(gè)開源的加解密解決方案,主頁在http://www.bouncycastle.org/
     Security.addProvider(new BouncyCastleProvider());
   }
 
   /**
   * AES解密
   *
   * @param data      //密文,被加密的數(shù)據(jù)
   * @param key      //秘鑰
   * @param iv       //偏移量
   * @param encodingFormat //解密后的結(jié)果需要進(jìn)行的編碼
  * @param type //0 是其他 1是微信步數(shù)
   * @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("小程序獲取用戶信息參數(shù)不能為空");
     //被加密的數(shù)據(jù)
     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("小程序解析出錯(cuò)1{}",e.getMessage());
     } catch (NoSuchPaddingException e) {
       e.printStackTrace();
       log.error("小程序解析出錯(cuò)2{}",e.getMessage());
     } catch (InvalidParameterSpecException e) {
       e.printStackTrace();
       log.error("小程序解析出錯(cuò)3{}",e.getMessage());
     } catch (InvalidKeyException e) {
       e.printStackTrace();
       log.error("小程序解析出錯(cuò)4{}",e.getMessage());
     } catch (InvalidAlgorithmParameterException e) {
       e.printStackTrace();
       log.error("小程序解析出錯(cuò)5{}",e.getMessage());
     } catch (IllegalBlockSizeException e) {
       e.printStackTrace();
       log.error("小程序解析出錯(cuò)6{}",e.getMessage());
     } catch (BadPaddingException e) {
       e.printStackTrace();
       log.error("小程序解析出錯(cuò)7{}",e.getMessage());
     }
  catch (UnsupportedEncodingException e) {
  e.printStackTrace();
  log.error("小程序解析出錯(cuò)8{}",e.getMessage());
  }
     return null;
   }
}

實(shí)現(xiàn)

@ApiOperation(value = "wx步數(shù)解密")
  @PostMapping(value = "/decode")
  public ResultModel<Object> questionList(@RequestBody WxSportParam param) throws Exception {
    HashMap<String, Object> map = wxXiaoChenXuUtil.getWxOpenId(//這個(gè)方法網(wǎng)上很多,沒有就用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);
  }

出來的數(shù)據(jù) :

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

tips:如果是解析用戶信息的話一樣的用法,解密decrypt中參數(shù)type傳0。兩者區(qū)別在于字節(jié)的decode方法不一樣而已。

到此這篇關(guān)于java微信小程序步數(shù)encryptedData和開放數(shù)據(jù)解密的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)java微信小程序步數(shù)encryptedData內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java8 CompletableFuture詳解

    Java8 CompletableFuture詳解

    這篇文章主要介紹了Java8 CompletableFuture詳解,CompletableFuture extends Future提供了方法,一元操作符和促進(jìn)異步性以及事件驅(qū)動(dòng)編程模型,需要的朋友可以參考下
    2014-06-06
  • CommonMark 使用教程:將 Markdown 語法轉(zhuǎn)成 Html

    CommonMark 使用教程:將 Markdown 語法轉(zhuǎn)成 Html

    這篇文章主要介紹了CommonMark 使用教程:將 Markdown 語法轉(zhuǎn)成 Html,這個(gè)技巧我們做任何網(wǎng)站都可以用到,而且非常好用。,需要的朋友可以參考下
    2019-06-06
  • 基于Spring Security的Oauth2授權(quán)實(shí)現(xiàn)方法

    基于Spring Security的Oauth2授權(quán)實(shí)現(xiàn)方法

    這篇文章主要介紹了基于Spring Security的Oauth2授權(quán)實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • SpringBoot結(jié)合Neo4j自定義cypherSql的方法

    SpringBoot結(jié)合Neo4j自定義cypherSql的方法

    這篇文章主要介紹了SpringBoot結(jié)合Neo4j自定義cypherSql,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-11-11
  • JAVA代碼書寫規(guī)范匯總詳解

    JAVA代碼書寫規(guī)范匯總詳解

    這篇文章主要介紹了JAVA代碼書寫規(guī)范匯總,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • springboot2.2 集成 activity6實(shí)現(xiàn)請假流程(示例詳解)

    springboot2.2 集成 activity6實(shí)現(xiàn)請假流程(示例詳解)

    這篇文章主要介紹了springboot2.2 集成 activity6實(shí)現(xiàn)請假完整流程示例詳解,本文通過示例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • tk-mybatis 的使用方法詳解

    tk-mybatis 的使用方法詳解

    tkmybatis是在mybatis框架的基礎(chǔ)上提供了很多工具,本文就詳細(xì)的介紹了一下tk-mybatis 的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • RocketMQ消息丟失的場景以及解決方案

    RocketMQ消息丟失的場景以及解決方案

    Apache RocketMQ是企業(yè)級的消息中間件,以其高性能和高可靠性而廣泛應(yīng)用,但是,消息丟失的問題在實(shí)踐中仍然存在,本文將探討此問題并提供解決方案,需要的朋友可以參考下
    2023-11-11
  • SpringBoot項(xiàng)目中使用Jsp的正確方法

    SpringBoot項(xiàng)目中使用Jsp的正確方法

    SpringBoot默認(rèn)是不支持JSP開發(fā)的,若是需要使用JSP的話便需要自己配置外部的tomcat,下面這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目中使用Jsp的正確方法,需要的朋友可以參考下
    2023-05-05
  • 使用SpringBoot的CommandLineRunner遇到的坑及解決

    使用SpringBoot的CommandLineRunner遇到的坑及解決

    這篇文章主要介紹了使用SpringBoot的CommandLineRunner遇到的坑及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02

最新評論