关于微信登陆态的信息解密和用户信息持久化

首先是获取微信URL中的信息类(给一个接口URL,返回接口中的信息字符串)

//获取网络URL接口信息工具类
public static String getURL(String URL) throws Exception{
    String result = "";
    String urlNameString=URL;
    URL realUrl = new URL(urlNameString); 
    URLConnection connection = realUrl.openConnection(); 
      HttpURLConnection httpURLConnection = null;
      if(connection instanceof HttpURLConnection){//能转化
httpURLConnection = (HttpURLConnection)connection;//强转
}
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"UTF-8"));//获取返回json 字符串流
String currentLineStr;
while((currentLineStr = bufferedReader.readLine()) != null){//读取行
result+=currentLineStr;
}
return result;  

}

2!获取的信息是被Base64,AES加密的,要解密出来,解密类

/**
* AES解密

* @param data
*            //密文,被加密的数据
* @param key
*            //秘钥
* @param iv
*            //偏移量
* @param encodingFormat
*            //解密后的结果需要进行的编码
* @return
* @throws Exception

*/

在解密是一定注意架包引的是否正确,参数是否接收正确。(之前犯了一个错误:java postman测试时,后台接收到的参数将加密数据字符串中的“+”号都变成了空格。这使得参数不正确了,但是在不测试的时候参数是正常的,“有些不明所以”)

public static String decrypt(String data, String key, String iv) throws Exception {
// 被加密的数据
byte[] dataByte = Base64.decodeBase64(data);
// 加密秘钥
byte[] keyByte = Base64.decodeBase64(key);
// 偏移量
byte[] ivByte = Base64.decodeBase64(iv);
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
Key sKeySpec = new SecretKeySpec(keyByte, "AES");
AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
params.init(new IvParameterSpec(ivByte));
cipher.init(Cipher.DECRYPT_MODE, sKeySpec, params);
byte[] resultByte = cipher.doFinal(dataByte);
if (null != resultByte && resultByte.length > 0) {
String result = new String(resultByte, "UTF-8");
return result;
}
return null;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;

}

3!拿到微信用户信息后,做本程序的用户持久化


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值