小程序
小游戏
企业微信
微信支付
扫描小程序码分享
java语言,写的解码方法,可以正确解析demo中的数据,但是在项目中 自己用的时候 解析后总是有乱码(很偶尔能正确解析)。
有遇到过的大神么?
3 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
时间过去太久了,我都忘记了还在这里问过。。。
贴一下代码吧,虽然,现在这个代码很容易找
import
javax.crypto.Cipher;
javax.crypto.spec.IvParameterSpec;
javax.crypto.spec.SecretKeySpec;
java.nio.charset.StandardCharsets;
/**
* Created by martyHou on 2018/11/9.
*/
public
class
WXBizDataCrypt {
private
String sessionKey;
WXBizDataCrypt(String sessionKey) {
this
.sessionKey = sessionKey;
}
String decryptData(String encryptedData, String ivData)
throws
Exception {
if
(
.sessionKey ==
null
||
.sessionKey.length() !=
24
) {
throw
new
Exception(
"invliad sessionKey"
);
byte
[] aesKey = base64Decode(
.sessionKey);
(ivData ==
|| ivData.length() !=
"invliad iv"
[] aesIV = base64Decode(ivData);
try
{
SecretKeySpec skeySpec =
SecretKeySpec(aesKey,
"AES"
Cipher cipher = Cipher.getInstance(
"AES/CBC/PKCS5Padding"
IvParameterSpec iv =
IvParameterSpec(aesIV);
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
[] encrypted1 = MyBase64.getDecodedBase64(encryptedData);
//先用base64解密
[] original = cipher.doFinal(encrypted1);
return
String(original, StandardCharsets.UTF_8);
catch
(Exception ex) {
ex.printStackTrace();
;
[] base64Decode(String base64Code) {
[] decoded =
[] bytes =base64Code.getBytes(StandardCharsets.UTF_8);
decoded = Base64.getDecoder().decode(bytes);
(Exception e) {
// TODO: handle exception
decoded;
static
void
main(String[] args)
String appId =
"myappid"
String sessionKey =
"mysessionkey"
String encryptedData =
"asdfafasdf"
String iv =
"r7BXXKkLb8qrSqiA=="
String res =
WXBizDataCrypt(sessionKey).decryptData(encryptedData, iv);
System.out.println(res);
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
您好,请问解决了么?能不能分享一下解码代码和jar包
打印一下小程序里发送到服务器之前的 encryptedData,和你服务器上收到的是否一直。
要加URI转码。
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
时间过去太久了,我都忘记了还在这里问过。。。
贴一下代码吧,虽然,现在这个代码很容易找
importjavax.crypto.Cipher;importjavax.crypto.spec.IvParameterSpec;importjavax.crypto.spec.SecretKeySpec;importjava.nio.charset.StandardCharsets;/*** Created by martyHou on 2018/11/9.*/publicclassWXBizDataCrypt {privateString sessionKey;publicWXBizDataCrypt(String sessionKey) {this.sessionKey = sessionKey;}publicString decryptData(String encryptedData, String ivData)throwsException {if(this.sessionKey ==null||this.sessionKey.length() !=24) {thrownewException("invliad sessionKey");}byte[] aesKey = base64Decode(this.sessionKey);if(ivData ==null|| ivData.length() !=24) {thrownewException("invliad iv");}byte[] aesIV = base64Decode(ivData);try{SecretKeySpec skeySpec =newSecretKeySpec(aesKey,"AES");Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");IvParameterSpec iv =newIvParameterSpec(aesIV);cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);byte[] encrypted1 = MyBase64.getDecodedBase64(encryptedData);//先用base64解密byte[] original = cipher.doFinal(encrypted1);returnnewString(original, StandardCharsets.UTF_8);}catch(Exception ex) {ex.printStackTrace();}returnnull;}privatebyte[] base64Decode(String base64Code) {byte[] decoded =null;try{byte[] bytes =base64Code.getBytes(StandardCharsets.UTF_8);decoded = Base64.getDecoder().decode(bytes);}catch(Exception e) {// TODO: handle exception}returndecoded;}publicstaticvoidmain(String[] args)throwsException {String appId ="myappid";String sessionKey ="mysessionkey";String encryptedData ="asdfafasdf";String iv ="r7BXXKkLb8qrSqiA==";String res =newWXBizDataCrypt(sessionKey).decryptData(encryptedData, iv);System.out.println(res);}}您好,请问解决了么?能不能分享一下解码代码和jar包
打印一下小程序里发送到服务器之前的 encryptedData,和你服务器上收到的是否一直。
要加URI转码。