微信小程序开发,解密算法官方没有JAVA示例DEMO么?
发布于 7 年前 作者 yxu 2168 次浏览 来自 问答

https://mp.weixin.qq.com/debug/wxadoc/dev/api/signature.html

微信官方提供了多种编程语言的示例代码(点击下载)。每种语言类型的接口名字均一致。调用方式可以参照示例。

下载发现没有JAVA示例代码,so what ??

4 回复

哈哈,谢谢楼上的,我试试。

二楼的William  ,导入的包可以发粗来看看么。AES ,Base64

initialize();

Key sKeySpec = new SecretKeySpec(keyByte, “AES”);

generateIV()

这些方法报错。

/** * 解密用户敏感数据

* [@param](/user/param) encryptedData 明文

* [@param](/user/param) iv            加密算法的初始向量

* [@param](/user/param) sessionId     会话ID

* [@return](/user/return)

*/

[@RequestMapping](/user/RequestMapping)(value = "/api/v1/wx/decodeUserInfo", method = RequestMethod.GET, produces = "application/json")

public Map<String,Object> decodeUserInfo(

       [@RequestParam](/user/RequestParam)(required = true,value = "encryptedData") String encryptedData,       

       [@RequestParam](/user/RequestParam)(required = true,value = "iv") String iv,       

       [@RequestParam](/user/RequestParam)(required = true,value = "sessionId") String sessionId){  

 

  //从缓存中获取session_key

    Object wxSessionObj = redisUtil.get(sessionId);   

   if(null == wxSessionObj){       

     return rtnParam(40008, null);

    }
    String wxSessionStr = (String)wxSessionObj;

    String sessionKey = wxSessionStr.split("#")[0];   


   try {

        AES aes = new AES();    

   

       byte[] resultByte = aes.decrypt(Base64.decodeBase64(encryptedData), Base64.decodeBase64(sessionKey), Base64.decodeBase64(iv));     

  

       if(null != resultByte && resultByte.length > 0){


            String userInfo = new String(resultByte, "UTF-8");    

       

           return rtnParam(0, userInfo);

        }


    } catch (InvalidAlgorithmParameterException e) {

        e.printStackTrace();


    } catch (UnsupportedEncodingException e) {

        e.printStackTrace();


    }   


   return rtnParam(50021, null);


}
 

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) {        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {        // TODO Auto-generated catch block
        e.printStackTrace();
    }

@William  initialize();generateIV();这两个方法不存在

回到顶部