如果大家使用小程序的同時(shí)還在使用公眾號(hào)的話,可能會(huì)用到unionId這種功能士鸥,由于公司業(yè)務(wù)需要驶臊,我們需要使用unionId,具體使用方法耘婚,請(qǐng)參考微信開(kāi)放平臺(tái)的說(shuō)明罢浇,但是在微信小程序的文檔中只給出了部分語(yǔ)言實(shí)現(xiàn)的源碼,竟然沒(méi)有java的沐祷,小程序的開(kāi)發(fā)人員是有多么懶嚷闭。難道大家都不用java寫(xiě)后臺(tái)?赖临?胞锰?
什么鬼,然后開(kāi)始了各種AES踩坑之路兢榨,其實(shí)參考了很多的網(wǎng)上的教程嗅榕,再次不能一一列出來(lái)給大家了顺饮,(因?yàn)槲覍?xiě)這篇文章的時(shí)候,已經(jīng)是解決問(wèn)題一周以后了)凌那,也收到管理員的很多幫助兼雄,再次寫(xiě)個(gè)帖子回饋大家吧,在此只列出unionId的解密方式帽蝶,如果有什么問(wèn)題赦肋,聯(lián)系我或者回帖都可以。
另外稍加吐槽一下励稳,
https 不要用startcom提供的免費(fèi)證書(shū)!
https 不要用startcom提供的免費(fèi)證書(shū)!
https 不要用startcom提供的免費(fèi)證書(shū)!
重要的事情說(shuō)三遍5璩恕!>阅帷趣避!
AES.java
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
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 AES {
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;
}
}
WxPKCS7Encoder.java
import java.nio.charset.Charset;
import java.util.Arrays;
/**
* Created by Kevin Dong on 2017/1/5.
*/
public class WxPKCS7Encoder {
private static final Charset CHARSET = Charset.forName("utf-8");
private static final int BLOCK_SIZE = 32;
/**
* 獲得對(duì)明文進(jìn)行補(bǔ)位填充的字節(jié).
*
* @param count 需要進(jìn)行填充補(bǔ)位操作的明文字節(jié)個(gè)數(shù)
* @return 補(bǔ)齊用的字節(jié)數(shù)組
*/
public static byte[] encode(int count) {
// 計(jì)算需要填充的位數(shù)
int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE);
if (amountToPad == 0) {
amountToPad = BLOCK_SIZE;
}
// 獲得補(bǔ)位所用的字符
char padChr = chr(amountToPad);
String tmp = new String();
for (int index = 0; index < amountToPad; index++) {
tmp += padChr;
}
return tmp.getBytes(CHARSET);
}
/**
* 刪除解密后明文的補(bǔ)位字符
*
* @param decrypted 解密后的明文
* @return 刪除補(bǔ)位字符后的明文
*/
public static byte[] decode(byte[] decrypted) {
int pad = decrypted[decrypted.length - 1];
if (pad < 1 || pad > 32) {
pad = 0;
}
return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad);
}
/**
* 將數(shù)字轉(zhuǎn)化成ASCII碼對(duì)應(yīng)的字符,用于對(duì)明文進(jìn)行補(bǔ)碼
*
* @param a 需要轉(zhuǎn)化的數(shù)字
* @return 轉(zhuǎn)化得到的字符
*/
public static char chr(int a) {
byte target = (byte) (a & 0xFF);
return (char) target;
}
}
調(diào)用方法解密如下:
WechatOpenIdRes wechatInfo = getWehatInfoByCode(code);
if(wechatInfo != null && wechatInfo.isOk()){
boolean isNew = true;
try {
AES aes = new AES();
byte[] resultByte = aes.decrypt(Base64.decodeBase64(encryptedData), Base64.decodeBase64(wechatInfo.getSession_key()), Base64.decodeBase64(iv));
if(null != resultByte && resultByte.length > 0){
String userInfo = new String(WxPKCS7Encoder.decode(resultByte));
WxInfo wxInfo = GsonUtil.fromGson(userInfo, WxInfo.class);
if(wxInfo != null) {
logger.debug("xxxxxunionid===="+wxInfo.getUnionId());
}
}
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
編譯環(huán)境為java1.8
另外我引入的support 包為
bcprov-jdk16-139.jar 此包已上傳附件,
順帶附上我試用的小程序js中的代碼吧扶欣,
var code ="";
wechat.login()
.then(function(res){
code = res.code;
})
.then(function(){
return wechat.getUserInfo();
})
.then(function(res){
var encryptedData = res.encryptedData
var iv = res.iv;
return userservice.getUserToken(code,encryptedData,iv);
})
上面的代碼使用了promise鹅巍,其中最后一句userservice.getUserToken為請(qǐng)求服務(wù)器的方法,參數(shù)為獲取到的code+加密內(nèi)容+初始化向量
有什么問(wèn)題可以聯(lián)系我料祠。
qq:403125094
源碼下載地址:http://www.wxapp-union.com/portal.php?mod=view&aid=1189