加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

微信小程序 用户数据解密详细介绍

发布时间:2020-12-14 20:22:42 所属栏目:资源 来源:网络整理
导读:微信小程序 用户数据解密 官方指引图: 引导图一步一步操作 1、获取code 2、发送code到第三方服务器,获取3rd_session 3、在第三方服务器上发送appid、appsecret、code到微信服务器换取session_key和openid 这里使用JFinal搭建的服务器 Redis配置 获取第三方

微信小程序 用户数据解密

官方指引图:

引导图一步一步操作

1、获取code

2、发送code到第三方服务器,获取3rd_session

3、在第三方服务器上发送appid、appsecret、code到微信服务器换取session_key和openid

这里使用JFinal搭建的服务器

Redis配置

获取第三方session

private void closeHttpClient(CloseableHttpClient client) throws IOException {
if (client != null) {
client.close();
}
}

ExecLinuxCMDUtil.Java

/**

  • java在linux环境下执行linux命令,然后返回命令返回值。
  • Created by LJaer on 16/12/22.
    */
    public class ExecLinuxCMDUtil {
    public static final ExecLinuxCMDUtil instance = new ExecLinuxCMDUtil();

public static Object exec(String cmd) {
try {
String[] cmdA = { "/bin/sh","-c",cmd };
Process process = Runtime.getRuntime().exec(cmdA);
LineNumberReader br = new LineNumberReader(new InputStreamReader(
process.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
sb.append(line).append("n");
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

4、解密用户数据

console输出结果:

后端解密代码

try {
byte[] resultByte = AESUtil.instance.decrypt(Base64.decodeBase64(encryptedData),Base64.decodeBase64(session_key),Base64.decodeBase64(iv));
if(null != resultByte && resultByte.length > 0){
String userInfo = new String(resultByte,"UTF-8");
System.out.println(userInfo);
JSONObject json = JSONObject.fromObject(userInfo); //将字符串{“id”:1}
renderJson(json);
}
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

AESUtil.java

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 AESUtil {
public static final AESUtil instance = new AESUtil();

public static boolean initialized = false;

/**

  • AES解密

  • @param content 密文

  • @return

  • @throws InvalidAlgorithmParameterException

  • @throws NoSuchProviderException
    */
    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();
    }
    return null;
    }

public static void initialize(){
if (initialized) return;
Security.addProvider(new BouncyCastleProvider());
initialized = true;
}
//生成iv
public static AlgorithmParameters generateIV(byte[] iv) throws Exception{
AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
params.init(new IvParameterSpec(iv));
return params;
}
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读