关于微信小程序中用户数据解密的介绍

这篇文章主要介绍了微信小程序 用户数据解密详细介绍的相关资料,需要的朋友可以参考下

微信小程序 用户数据解密

官方指引图:

引导图一步一步操作

1、获取code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

1源码搭建wcqh.cn7

18

19

20

onLoad: function (options) {

// 页面初始化 options为页面跳转所带来的参数

let that = this

wx.login({

success: func源码搭建wcqh.cntion (res) {

// success

let code = res.code

that.setData({ code: code })

wx.getUserInfo({

success: functi源码搭建wcqh.cnon (res) {

// success

that.setData({ userInfo: res.userInfo })

that.setData({ iv: res.iv })

that.setData源码搭建wcqh.cn({ encryptedData: res.encryptedData })

that.get3rdSession()

}

})

}

})

}

登录后复制

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

1源码搭建wcqh.cn

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

get3rdSession:function(){

let that = this

wx.request({

url: 'https://loca源码搭建wcqh.cnlhost:8443/get3rdSession',

data: {

code: this.data.code

},

method: 'GET', // OPTIONS, GET, H源码搭建wcqh.cnEAD, POST, PUT, DELETE, TRACE, CONNECT

// header: {}, // 设置请求的 header

success: function (res) {

// succ源码搭建wcqh.cness

var sessionId = res.data.session;

that.setData({ sessionId: sessionId })

wx.setStorageSync('ses源码搭建wcqh.cnsionId', sessionId)

that.decodeUserInfo()

}

})

}

登录后复制

3、在第三方服务器上发送appid、appsecret、code到微信服务器换取session源码搭建wcqh.cn_key和openid

这里使用JFinal搭建的服务器

Redis配置

1

2

3

4

5

public void configPlugin(Plugins me) {

//用于缓存userinfo模块的redis服源码搭建wcqh.cn

RedisPlugin userInfoRedis = new RedisPlugin(“userInfo”,”localhost”);

me.add(userInfoRedis);

}

登录后复制

获取第源码搭建wcqh.cn三方session

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50源码搭建wcqh.cn

51

52

53

54

public void get3rdSession() {

//获取名为userInfo的Redis Cache对象

Cache userInfoCache = Redis.use(“us源码搭建wcqh.cnerInfo”);

String sessionId = “”;

JSONObject json = new JSONObject();

String code = getPara(“code”);

Stri源码搭建wcqh.cnng url = “https://api.weixin.qq.com/sns/jscode2session?appid=wx7560b8008e2c445d&secret=f1af3312b7038源码搭建wcqh.cn513fd17dd9cbc3b357c&js_code=” + code + “&grant_type=authorization_code”;

//执行命令生成3rd_session

String se源码搭建wcqh.cnssion = ExecLinuxCMDUtil.instance.exec(“cat /dev/urandom |od -x | tr -d ' '| head -n 1”).toS源码搭建wcqh.cntring();

json.put(“session”, session);

//创建默认的httpClient实例

CloseableHttpClient httpClient = getHttpClie源码搭建wcqh.cnnt();

try {

//用get方法发送http请求

HttpGet get = new HttpGet(url);

System.out.println(“执行get请求:….” + get.get源码搭建wcqh.cnURI());

CloseableHttpResponse httpResponse = null;

//发送get请求

httpResponse = httpClient.execute(get);

try源码搭建wcqh.cn {

//response实体

HttpEntity entity = httpResponse.getEntity();

if (null != entity) {

String result = Enti源码搭建wcqh.cntyUtils.toString(entity);

System.out.println(result);

JSONObject resultJson = JSONObject.fromObject(re源码搭建wcqh.cnsult);

String session_key = resultJson.getString(“session_key”);

String openid = resultJson.getString(源码搭建wcqh.cn“openid”);

//session存储

userInfoCache.set(session,session_key+”,”+openid);

}

} finally {

httpResponse.clos源码搭建wcqh.cne();

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

closeHttpClient(httpClient);

} catch 源码搭建wcqh.cn(IOException e) {

e.printStackTrace();

}

}

renderJson(json);

}

private CloseableHttpClient getHttpClient()源码搭建wcqh.cn {

return HttpClients.createDefault();

}

private void closeHttpClient(CloseableHttpClient client) throw源码搭建wcqh.cns IOException {

if (client != null) {

client.close();

}

}

登录后复制

ExecLinuxCMDUtil.Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15源码搭建wcqh.cn

16

17

18

19

20

21

22

23

24

25

26

27

28

29

import java.io.InputStreamReader;

import java.io.LineNumberReader;

/**

* ja源码搭建wcqh.cnva在linux环境下执行linux命令,然后返回命令返回值。

* Created by LJaer on 16/12/22.

*/

public class ExecLinuxCMDUtil {

publi源码搭建wcqh.cnc static final ExecLinuxCMDUtil instance = new ExecLinuxCMDUtil();

public static Object exec(String c源码搭建wcqh.cnmd) {

try {

String[] cmdA = { “/bin/sh”, “-c”, cmd };

Process process = Runtime.getRuntime().exec(cmdA)源码搭建wcqh.cn;

LineNumberReader br = new LineNumberReader(new InputStreamReader(

process.getInputStream()));

StringB源码搭建wcqh.cnuffer sb = new StringBuffer();

String line;

while ((line = br.readLine()) != null) {

System.out.println源码搭建wcqh.cn(line);

sb.append(line).append(“\n”);

}

return sb.toString();

} catch (Exception e) {

e.printStackTrace()源码搭建wcqh.cn;

}

return null;

}

}

登录后复制

4、解密用户数据

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

decodeUserInfo:function(){

let that = this

wx.re源码搭建wcqh.cnquest({

url: 'https://localhost:8443/decodeUserInfo',

data: {

encryptedData: that.data.encrypte源码搭建wcqh.cndData,

iv: that.data.iv,

session: wx.getStorageSync('sessionId')

},

method: 'GET', // OP源码搭建wcqh.cnTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT

// header: {}, // 设置请求的 header

success: function (源码搭建wcqh.cnres) {

// success

console.log(res)

}

})

}

登录后复制

console输出结果:

后端解密代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25源码搭建wcqh.cn

26

27

28

29

30

31

32

/**

* 解密用户敏感数据

*/

public void decodeUserInfo(){

String encryptedData = getPara(“encryptedD源码搭建wcqh.cnata”);

String iv = getPara(“iv”);

String session = getPara(“session”);

//从缓存中获取session_key

//获取名称为userIn源码搭建wcqh.cnfo的Redis Cache对象

Cache userInfoRedis = Redis.use(“userInfo”);

Object wxSessionObj = userInfoRedis.get(源码搭建wcqh.cnsession);

if(null==wxSessionObj){

renderNull();

}

String wxSessionStr = (String)wxSessionObj;

String sess源码搭建wcqh.cnion_key = wxSessionStr.split(“,”)[0];

try {

byte[] resultByte = AESUtil.instance.decrypt(Base64.decode源码搭建wcqh.cnBase64(encryptedData), Base64.decodeBase64(session_key), Base64.decodeBase64(iv));

if(null != resultB源码搭建wcqh.cnyte && resultByte.length > 0){

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

System.out.println(u源码搭建wcqh.cnserInfo);

JSONObject json = JSONObject.fromObject(userInfo); //将字符串{“id”:1}

renderJson(json);

}

} catch 源码搭建wcqh.cn(InvalidAlgorithmParameterException e) {

e.printStackTrace();

} catch (UnsupportedEncodingException e)源码搭建wcqh.cn {

e.printStackTrace();

}

}

登录后复制

AESUtil.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34源码搭建wcqh.cn

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

import org.bouncycastle.jce.provider.Bounc源码搭建wcqh.cnyCastleProvider;

import javax.crypto.BadPaddingException;

import javax.crypto.Cipher;

import javax.cryp源码搭建wcqh.cnto.IllegalBlockSizeException;

import javax.crypto.NoSuchPaddingException;

import javax.crypto.spec.IvP源码搭建wcqh.cnarameterSpec;

import javax.crypto.spec.SecretKeySpec;

import java.security.*;

public class AESUtil {

pub源码搭建wcqh.cnlic static final AESUtil instance = new AESUtil();

public static boolean initialized = false;

/**

* AES源码搭建wcqh.cn解密

* @param content 密文

* @return

* @throws InvalidAlgorithmParameterException

* @throws NoSuchProviderEx源码搭建wcqh.cnception

*/

public byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) throws InvalidAlgorith源码搭建wcqh.cnmParameterException {

initialize();

try {

Cipher cipher = Cipher.getInstance(“AES/CBC/PKCS7Padding”);

Ke源码搭建wcqh.cny sKeySpec = new SecretKeySpec(keyByte, “AES”);

cipher.init(Cipher.DECRYPT_MODE, sKeySpec, generateIV源码搭建wcqh.cn(ivByte));// 初始化

byte[] result = cipher.doFinal(content);

return result;

} catch (NoSuchAlgorithmExcept源码搭建wcqh.cnion e) {

e.printStackTrace();

} catch (NoSuchPaddingException e) {

e.printStackTrace();

} catch (Invalid源码搭建wcqh.cnKeyException e) {

e.printStackTrace();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

} ca源码搭建wcqh.cntch (BadPaddingException e) {

e.printStackTrace();

} catch (NoSuchProviderException e) {

// TODO Auto-g源码搭建wcqh.cnenerated catch block

e.printStackTrace();

} catch (Exception e) {

// TODO Auto-generated catch block

e.p源码搭建wcqh.cnrintStackTrace();

}

return null;

}

public static void initialize(){

if (initialized) return;

Security.addP源码搭建wcqh.cnrovider(new BouncyCastleProvider());

initialized = true;

}

//生成iv

public static AlgorithmParameters gene源码搭建wcqh.cnrateIV(byte[] iv) throws Exception{

AlgorithmParameters params = AlgorithmParameters.getInstance(“AES源码搭建wcqh.cn“);

params.init(new IvParameterSpec(iv));

return params;

}

}

登录后复制

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文源码搭建wcqh.cn网!

相关推荐:

微信小程序通过保存图片分享到朋友圈的功能实现

关于微信小程序收藏功能的实现

微信小程序如何获取openid及用户信息

以上就是关于微信小程序中用户数据解密的介绍的详细内容,更多请关注php中文源码搭建wcqh.cn网其它相关文章!

© 版权声明
THE END
喜欢就支持一下吧
点赞129 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容