標(biāo)簽:微信小程序 用戶信息
小程序中衣形,通過調(diào)用微信API(wx.getUserInfo)可以或得到當(dāng)前用戶的微信信息(此處會(huì)彈出獲取信息授權(quán)框),但是僅有用戶的基本信息兔院,比如頭像殖卑、昵稱、性別等坊萝,由于產(chǎn)品業(yè)務(wù)需要孵稽,需要得到當(dāng)前用戶的openid以及unionId(微信開放平臺(tái)下用戶的唯一標(biāo)識(shí)),那如何獲仁肌菩鲜?其實(shí)微信的API(wx.getUserInfo)中,已經(jīng)給到這兩組數(shù)據(jù)扯键,只是為了安全睦袖,沒有在明文中顯示珊肃,而是在wx.getUserInfo方法的結(jié)果參數(shù)encryptedData 中荣刑,只要將參數(shù)encryptedData 進(jìn)行解密,就可以得到想要的數(shù)據(jù)伦乔,官方?jīng)]有給到Java的解密過程厉亏,下面是Java版的解密過程:
package com.boboqi.wx.userinfo;
import java.io.UnsupportedEncodingException;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.security.spec.InvalidParameterSpecException;
import java.util.HashMap;
import java.util.Map;
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 net.sf.json.JSONObject;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.Arrays;
import org.codehaus.xfire.util.Base64;
import com.boboqi.wx.common.RemoteInterfaceAddress;
import com.boboqi.wx.util.HttpsClientUtil;
/**
* 微信小程序信息獲取
* @author songhn
*/
public class WXAppletUserInfo {
/**
* 獲取微信小程序 session_key 和 openid
*/
public JSONObject getSessionKeyAndOropenid(String code){
//微信端登錄code值
String wxCode = code;
String requestUrl = WxappConfig.GETSESSIONKEYOROPENID;
Map<String,String> requestUrlParam = new HashMap<String,String>();
requestUrlParam.put("appid", RemoteInterfaceAddress.AppletAPPID);
requestUrlParam.put("secret", RemoteInterfaceAddress.AppletAppSecret);
requestUrlParam.put("js_code", wxCode);
requestUrlParam.put("grant_type", "authorization_code");
JSONObject jsonObject = HttpsClientUtil.getInstance().sendGetRequest(requestUrl, requestUrlParam);
return jsonObject;
}
/**
* 解密用戶敏感數(shù)據(jù)獲取用戶信息
* @param sessionKey 數(shù)據(jù)進(jìn)行加密簽名的密鑰
* @param encryptedData 包括敏感數(shù)據(jù)在內(nèi)的完整用戶信息的加密數(shù)據(jù)
* @param iv 加密算法的初始向量
*/
public JSONObject getUserInfo(String encryptedData,String sessionKey,String iv){
// 被加密的數(shù)據(jù)
byte[] dataByte = Base64.decode(encryptedData);
// 加密秘鑰
byte[] keyByte = Base64.decode(sessionKey);
// 偏移量
byte[] ivByte = Base64.decode(iv);
try {
// 如果密鑰不足16位,那么就補(bǔ)足. 這個(gè)if 中的內(nèi)容很重要
int base = 16;
if (keyByte.length % base != 0) {
int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
keyByte = temp;
}
// 初始化
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding","BC");
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) {
String result = new String(resultByte, "UTF-8");
return JSONObject.fromObject(result);
}
}catch (NoSuchProviderException e) {
e.printStackTrace();
}
return null;
}
}
附:微信小程序加密數(shù)據(jù)解密算法
接口如果涉及敏感數(shù)據(jù)(如wx.getUserInfo
當(dāng)中的 openId 和unionId )烈和,接口的明文內(nèi)容將不包含這些敏感數(shù)據(jù)爱只。開發(fā)者如需要獲取敏感數(shù)據(jù),需要對(duì)接口返回的加密數(shù)據(jù)( encryptedData )進(jìn)行對(duì)稱解密招刹。 解密算法如下:
對(duì)稱解密使用的算法為 AES-128-CBC恬试,數(shù)據(jù)采用PKCS#7填充。
對(duì)稱解密的目標(biāo)密文為 Base64_Decode(encryptedData)疯暑。
對(duì)稱解密秘鑰 aeskey = Base64_Decode(session_key), aeskey 是16字節(jié)训柴。
對(duì)稱解密算法初始向量 為Base64_Decode(iv),其中iv由數(shù)據(jù)接口返回妇拯。
另外幻馁,為了應(yīng)用能校驗(yàn)數(shù)據(jù)的有效性洗鸵,我們會(huì)在敏感數(shù)據(jù)加上數(shù)據(jù)水印( watermark )