如果大家使用小程序的同时还在使用公众号的话,可能会用到unionid这种功能,由于公司业务需要,我们需要使用unionid,具体使用方法,请参考微信开放平台的说明,但是在微信小程序的文档中只给出了部分语言实现的源码,竟然没有java的,小程序的开发人员是有多么懒。难道大家都不用java写后台???
什么鬼,然后开始了各种aes踩坑之路,其实参考了很多的网上的教程,再次不能一一列出来给大家了,(因为我写这篇文章的时候,已经是解决问题一周以后了),也收到管理员的很多帮助,再次写个帖子回馈大家吧,在此只列出unionid的解密方式,如果有什么问题,联系我或者回帖都可以。
另外稍加吐槽一下,
https 不要用startcom提供的免费证书!
https 不要用startcom提供的免费证书!
https 不要用startcom提供的免费证书!
重要的事情说三遍!!!!
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; /**
* 获得对明文进行补位填充的字节.
*
* @param count 需要进行填充补位操作的明文字节个数
* @return 补齐用的字节数组
*/
public static byte[] encode(int count) { // 计算需要填充的位数
int amounttopad = block_size - (count % block_size); if (amounttopad == 0) {
amounttopad = block_size;
} // 获得补位所用的字符
char padchr = chr(amounttopad);
string tmp = new string(); for (int index = 0; index < amounttopad; index++) {
tmp += padchr;
} return tmp.getbytes(charset);
} /**
* 删除解密后明文的补位字符
*
* @param decrypted 解密后的明文
* @return 删除补位字符后的明文
*/
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);
} /**
* 将数字转化成ascii码对应的字符,用于对明文进行补码
*
* @param a 需要转化的数字
* @return 转化得到的字符
*/
public static char chr(int a) { byte target = (byte) (a & 0xff); return (char) target;
}
}
调用方法解密如下:
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();
}
编译环境为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.encrypteddatavar iv = res.iv;return userservice.getusertoken(code,encrypteddata,iv);
})
上面的代码使用了promise,其中最后一句userservice.getusertoken为请求服务器的方法,参数为获取到的code+加密内容+初始化向量
更多微信小程序java实现aes解密并获取unionid。