Util.java
```
publicclassUtil{
privatestaticfinalMediaTypeJSON=MediaType.parse("application/json;charset=utf-8");
privatestaticfinalStringWXGAME_URL="https://mp.weixin.qq.com/wxagame/wxagame_settlement";
publicstaticfinalStringSESSIONID_ERROR="SESSIONID有误,请检查";
privatestaticStringgetActionData(StringsessionKey,StringencryptedData,Stringiv) {
byte[] sessionKeyBy=sessionKey.getBytes();
byte[] en=newbyte[0];
try{
en=encryptedData.getBytes("UTF-8");
}catch(UnsupportedEncodingExceptione) {
e.printStackTrace();
}
byte[] ivBy=iv.getBytes();
byte[] enc=Pkcs7Encoder.encryptOfDiyIV(en, sessionKeyBy, ivBy);
returnnewString(Base64.toBase64String(enc));
}
publicstaticStringpostData(Stringscore,Stringtimes,Stringsession_id) {
Stringresult=null;
Stringcontent="{\"score\":"+score+",\"times\":"+times+"}";
StringAES_KEY=null;
try{
AES_KEY=session_id.substring(0,16);
}catch(Exceptione) {
returnSESSIONID_ERROR;
}
StringAES_IV=AES_KEY;
OkHttpClientokHttpClient=newOkHttpClient();
StringactionData=Util.getActionData(AES_KEY, content,AES_IV);
Stringjson="{\"base_req\":{\"session_id\":\""+session_id+"\",\"fast\":1},\"action_data\":\""+actionData+"\"}";
RequestBodyrequestBody=RequestBody.create(JSON, json);
Requestrequest=newRequest.Builder()
.url(WXGAME_URL)
.header("Accept","*/*")
.header("Accept-Language","zh-cn")
.header("User-Agent","Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Mobile/14E304 MicroMessenger/6.6.1 NetType/WIFI Language/zh_CN")
.header("Content-Length","680")
.header("Content-Type","application/json")
.header("Referer","https://servicewechat.com/wx7c8d593b2c3a7703/5/page-frame.html")
.header("Host","mp.weixin.qq.com")
.header("Connection","keep-alive")
.post(requestBody)
.build();
ResponseBodyresponseBody=null;
try{
responseBody=okHttpClient.newCall(request).execute().body();
result=responseBody.string();
}catch(IOExceptione) {
e.printStackTrace();
}finally{
if(responseBody!=null) {
responseBody.close();
}
}
returnresult;
}
}
```
Pkcs7Encoder.java
```
publicclassPkcs7Encoder{
//算法名称
staticfinalStringKEY_ALGORITHM="AES";
//加解密算法/模式/填充方式
staticfinalStringALGORITHMSTR="AES/CBC/PKCS7Padding";
privatestaticKeykey;
privatestaticCiphercipher;
booleanisInited=false;
//默认对称解密算法初始向量 iv
staticbyte[] iv={0x30,0x31,0x30,0x32,0x30,0x33,0x30,0x34,0x30,0x35,0x30,0x36,0x30,0x37,0x30,0x38};
publicstaticvoidinit(byte[]keyBytes) {
//如果密钥不足16位,那么就补足. 这个if 中的内容很重要
intbase=16;
if(keyBytes.length%base!=0) {
intgroups=keyBytes.length/base+(keyBytes.length%base!=0?1:0);
byte[] temp=newbyte[groups*base];
Arrays.fill(temp, (byte)0);
System.arraycopy(keyBytes,0, temp,0, keyBytes.length);
keyBytes=temp;
}
//初始化
Security.addProvider(newBouncyCastleProvider());
//转化成JAVA的密钥格式
key=newSecretKeySpec(keyBytes,KEY_ALGORITHM);
try{
//初始化cipher
cipher=Cipher.getInstance(ALGORITHMSTR,"BC");
}catch(NoSuchAlgorithmExceptione) {
//TODO Auto-generated catch block
e.printStackTrace();
}catch(NoSuchPaddingExceptione) {
//TODO Auto-generated catch block
e.printStackTrace();
}catch(NoSuchProviderExceptione) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 加密方法
* --使用默认iv时
* @param content
* 要加密的字符串
* @param keyBytes
* 加密密钥
* @return
*/
publicstaticbyte[]encrypt(byte[]content,byte[]keyBytes) {
byte[] encryptedText=encryptOfDiyIV(content,keyBytes,iv);
returnencryptedText;
}
/**
* 解密方法
* --使用默认iv时
* @param encryptedData
* 要解密的字符串
* @param keyBytes
* 解密密钥
* @return
*/
publicstaticbyte[]decrypt(byte[]encryptedData,byte[]keyBytes) {
byte[] encryptedText=decryptOfDiyIV(encryptedData,keyBytes,iv);
returnencryptedText;
}
/**
* 加密方法
* ---自定义对称解密算法初始向量 iv
* @param content
* 要加密的字符串
* @param keyBytes
* 加密密钥
* @param ivs
* 自定义对称解密算法初始向量 iv
* @return 加密的结果
*/
publicstaticbyte[]encryptOfDiyIV(byte[]content,byte[]keyBytes,byte[]ivs) {
byte[] encryptedText=null;
init(keyBytes);
try{
cipher.init(Cipher.ENCRYPT_MODE, key,newIvParameterSpec(ivs));
encryptedText=cipher.doFinal(content);
}catch(Exceptione) {
//TODO Auto-generated catch block
e.printStackTrace();
}
returnencryptedText;
}
/**
* 解密方法
*
* @param encryptedData
* 要解密的字符串
* @param keyBytes
* 解密密钥
* @param ivs
* 自定义对称解密算法初始向量 iv
* @return
*/
publicstaticbyte[]decryptOfDiyIV(byte[]encryptedData,byte[]keyBytes,byte[]ivs) {
byte[] encryptedText=null;
init(keyBytes);
try{
cipher.init(Cipher.DECRYPT_MODE, key,newIvParameterSpec(ivs));
encryptedText=cipher.doFinal(encryptedData);
}catch(Exceptione) {
//TODO Auto-generated catch block
e.printStackTrace();
}
returnencryptedText;
}
}
```
网友评论