时间过去太久了,我都忘记了还在这里问过。。。 贴一下代码吧,虽然,现在这个代码很容易找 [代码]import[代码] [代码]javax.crypto.Cipher;[代码][代码]import[代码] [代码]javax.crypto.spec.IvParameterSpec;[代码][代码]import[代码] [代码]javax.crypto.spec.SecretKeySpec;[代码][代码]import[代码] [代码]java.nio.charset.StandardCharsets;[代码][代码] [代码] [代码]/**[代码][代码] [代码][代码]* Created by martyHou on 2018/11/9.[代码][代码] [代码][代码]*/[代码][代码]public[代码] [代码]class[代码] [代码]WXBizDataCrypt {[代码][代码] [代码][代码]private[代码] [代码]String sessionKey;[代码][代码] [代码] [代码] [代码][代码]public[代码] [代码]WXBizDataCrypt(String sessionKey) {[代码][代码] [代码][代码]this[代码][代码].sessionKey = sessionKey;[代码][代码] [代码][代码]}[代码][代码] [代码] [代码] [代码][代码]public[代码] [代码]String decryptData(String encryptedData, String ivData) [代码][代码]throws[代码] [代码]Exception {[代码][代码] [代码] [代码] [代码][代码]if[代码] [代码]([代码][代码]this[代码][代码].sessionKey == [代码][代码]null[代码] [代码]|| [代码][代码]this[代码][代码].sessionKey.length() != [代码][代码]24[代码][代码]) {[代码][代码] [代码][代码]throw[代码] [代码]new[代码] [代码]Exception([代码][代码]"invliad sessionKey"[代码][代码]);[代码][代码] [代码][代码]}[代码][代码] [代码] [代码] [代码][代码]byte[代码][代码][] aesKey = base64Decode([代码][代码]this[代码][代码].sessionKey);[代码][代码] [代码][代码]if[代码] [代码](ivData == [代码][代码]null[代码] [代码]|| ivData.length() != [代码][代码]24[代码][代码]) {[代码][代码] [代码][代码]throw[代码] [代码]new[代码] [代码]Exception([代码][代码]"invliad iv"[代码][代码]);[代码][代码] [代码][代码]}[代码][代码] [代码][代码]byte[代码][代码][] aesIV = base64Decode(ivData);[代码][代码] [代码] [代码] [代码][代码]try[代码] [代码]{[代码][代码] [代码][代码]SecretKeySpec skeySpec = [代码][代码]new[代码] [代码]SecretKeySpec(aesKey, [代码][代码]"AES"[代码][代码]);[代码][代码] [代码][代码]Cipher cipher = Cipher.getInstance([代码][代码]"AES/CBC/PKCS5Padding"[代码][代码]);[代码][代码] [代码][代码]IvParameterSpec iv = [代码][代码]new[代码] [代码]IvParameterSpec(aesIV);[代码][代码] [代码][代码]cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);[代码][代码] [代码][代码]byte[代码][代码][] encrypted1 = MyBase64.getDecodedBase64(encryptedData);[代码][代码]//先用base64解密[代码][代码] [代码][代码]byte[代码][代码][] original = cipher.doFinal(encrypted1);[代码][代码] [代码][代码]return[代码] [代码]new[代码] [代码]String(original, StandardCharsets.UTF_8);[代码][代码] [代码][代码]} [代码][代码]catch[代码] [代码](Exception ex) {[代码][代码] [代码][代码]ex.printStackTrace();[代码][代码] [代码][代码]}[代码][代码] [代码][代码]return[代码] [代码]null[代码][代码];[代码][代码] [代码][代码]}[代码][代码] [代码] [代码] [代码] [代码] [代码][代码]private[代码] [代码]byte[代码][代码][] 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[代码][代码] [代码][代码]}[代码][代码] [代码][代码]return[代码] [代码]decoded;[代码][代码] [代码][代码]}[代码][代码] [代码] [代码] [代码][代码]public[代码] [代码]static[代码] [代码]void[代码] [代码]main(String[] args) [代码][代码]throws[代码] [代码]Exception {[代码][代码] [代码][代码]String appId = [代码][代码]"myappid"[代码][代码];[代码][代码] [代码][代码]String sessionKey = [代码][代码]"mysessionkey"[代码][代码];[代码][代码] [代码][代码]String encryptedData = [代码][代码]"asdfafasdf"[代码][代码];[代码][代码] [代码][代码]String iv = [代码][代码]"r7BXXKkLb8qrSqiA=="[代码][代码];[代码][代码] [代码][代码]String res = [代码][代码]new[代码] [代码]WXBizDataCrypt(sessionKey).decryptData(encryptedData, iv);[代码][代码] [代码][代码]System.out.println(res);[代码][代码] [代码][代码]}[代码][代码]}[代码]
encryptedData无法被正确解码java语言,写的解码方法,可以正确解析demo中的数据,但是在项目中 自己用的时候 解析后总是有乱码(很偶尔能正确解析)。 有遇到过的大神么?
2019-09-18