java微信小程序步数encryptedData和开放数据解密的实现
前提:
三个参数,
1.sessionKey(拿openId的时候可以得到)
2.encryptedData(前端提供)
3.iv(前端提供)
一个类,一个方法。
1.类:
importjava.nio.charset.Charset;
importjava.util.Arrays;
/**
*微信小程序加解密
*@authorliuyazhuang
*
*/
publicclassWxPKCS7Encoder{
privatestaticfinalCharsetCHARSET=Charset.forName("utf-8");
privatestaticfinalintBLOCK_SIZE=32;
/**
*获得对明文进行补位填充的字节.
*
*@paramcount
*需要进行填充补位操作的明文字节个数
*@return补齐用的字节数组
*/
publicstaticbyte[]encode(intcount){
//计算需要填充的位数
intamountToPad=BLOCK_SIZE-(count%BLOCK_SIZE);
if(amountToPad==0){
amountToPad=BLOCK_SIZE;
}
//获得补位所用的字符
charpadChr=chr(amountToPad);
Stringtmp=newString();
for(intindex=0;index32){
pad=0;
}
returnArrays.copyOfRange(decrypted,0,decrypted.length-pad);
}
/**
*将数字转化成ASCII码对应的字符,用于对明文进行补码
*
*@parama
*需要转化的数字
*@return转化得到的字符
*/
publicstaticcharchr(inta){
bytetarget=(byte)(a&0xFF);
return(char)target;
}
}
2.方法:
importjava.io.UnsupportedEncodingException;
importjava.security.AlgorithmParameters;
importjava.security.InvalidAlgorithmParameterException;
importjava.security.InvalidKeyException;
importjava.security.NoSuchAlgorithmException;
importjava.security.Security;
importjava.security.spec.InvalidParameterSpecException;
importjava.util.HashMap;
importjavax.annotation.Resource;
importjavax.crypto.BadPaddingException;
importjavax.crypto.Cipher;
importjavax.crypto.IllegalBlockSizeException;
importjavax.crypto.NoSuchPaddingException;
importjavax.crypto.spec.IvParameterSpec;
importjavax.crypto.spec.SecretKeySpec;
importorg.apache.commons.codec.binary.Base64;
importorg.apache.commons.lang3.StringUtils;
importorg.bouncycastle.jce.provider.BouncyCastleProvider;
importlombok.extern.slf4j.Slf4j;
@Slf4j
publicclassAesCbcUtil{
static{
//BouncyCastle是一个开源的加解密解决方案,主页在http://www.bouncycastle.org/
Security.addProvider(newBouncyCastleProvider());
}
/**
*AES解密
*
*@paramdata//密文,被加密的数据
*@paramkey//秘钥
*@paramiv//偏移量
*@paramencodingFormat//解密后的结果需要进行的编码
*@paramtype//0是其他1是微信步数
*@return
*@throwsException
*/
publicstaticStringdecrypt(Stringdata,Stringkey,Stringiv,StringencodingFormat,Integertype)throwsException{
//initialize();
if(StringUtils.isEmpty(data)||StringUtils.isEmpty(key)||StringUtils.isEmpty(iv))
thrownewSkyParamNullException("小程序获取用户信息参数不能为空");
//被加密的数据
byte[]dataByte=Base64.decodeBase64(data);
//加密秘钥
byte[]keyByte=Base64.decodeBase64(key);
//偏移量
byte[]ivByte=Base64.decodeBase64(iv);
try{
Ciphercipher=Cipher.getInstance("AES/CBC/PKCS7Padding");
SecretKeySpecspec=newSecretKeySpec(keyByte,"AES");
AlgorithmParametersparameters=AlgorithmParameters.getInstance("AES");
parameters.init(newIvParameterSpec(ivByte));
cipher.init(Cipher.DECRYPT_MODE,spec,parameters);//初始化
byte[]resultByte=cipher.doFinal(dataByte);
if(null!=resultByte&&resultByte.length>0){
if(type==1){
returnnewString(WxPKCS7Encoder.decode(resultByte));
}else{
returnnewString(resultByte,encodingFormat);
}
}
returnnull;
}catch(NoSuchAlgorithmExceptione){
e.printStackTrace();
log.error("小程序解析出错1{}",e.getMessage());
}catch(NoSuchPaddingExceptione){
e.printStackTrace();
log.error("小程序解析出错2{}",e.getMessage());
}catch(InvalidParameterSpecExceptione){
e.printStackTrace();
log.error("小程序解析出错3{}",e.getMessage());
}catch(InvalidKeyExceptione){
e.printStackTrace();
log.error("小程序解析出错4{}",e.getMessage());
}catch(InvalidAlgorithmParameterExceptione){
e.printStackTrace();
log.error("小程序解析出错5{}",e.getMessage());
}catch(IllegalBlockSizeExceptione){
e.printStackTrace();
log.error("小程序解析出错6{}",e.getMessage());
}catch(BadPaddingExceptione){
e.printStackTrace();
log.error("小程序解析出错7{}",e.getMessage());
}
catch(UnsupportedEncodingExceptione){
e.printStackTrace();
log.error("小程序解析出错8{}",e.getMessage());
}
returnnull;
}
}
实现
@ApiOperation(value="wx步数解密") @PostMapping(value="/decode") publicResultModel
出来的数据:
{“stepInfoList”:[
{
“timestamp”:1445866601,
“step”:100
},
{
“timestamp”:1445876601,
“step”:120
}]}
tips:如果是解析用户信息的话一样的用法,解密decrypt中参数type传0。两者区别在于字节的decode方法不一样而已。
到此这篇关于java微信小程序步数encryptedData和开放数据解密的实现的文章就介绍到这了,更多相关java微信小程序步数encryptedData内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!