/**
* 对微信的返回数据进行解密 做AES-256-ECB解密(PKCS7Padding)
* @param encrypted 目标密文
* @param aesKey 解密密钥
* @param
* @throws Exception
*/
public static String decryptWXReturn(String encrypted, String aesKey){
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding","BC"); //这里是不是这样写?
//对秘钥进行MD5编码
//这里生成key的方法是错的
Key key = initKeyForAES(MD5Util.encode(aesKey).toLowerCase().toString());
//这才是生成key的正确方法
SecretKeySpec key = new SecretKeySpec(MD5Utils.MD5Encode(aesKey,"UTF-8").toLowerCase().toLowerCase().getBytes(), "AES");
//对秘钥进行MD5加密的时候必须指定UTF-8编码,这样写也是不行的
SecretKeySpec key = new SecretKeySpec(MD5Util.encode(aesKey).toLowerCase().toLowerCase().getBytes(), "AES"); //这样也会报错
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] encryptedBase64 = new BASE64Decoder().decodeBuffer(encrypted);
byte[] decbbdt = cipher.doFinal(encryptedBase64); //到这一步就开始报错pad block corrupted
return new String(decbbdt,"UTF-8");
} catch (Exception e) {
logger.error(e);
}
return null;
}
public static Key initKeyForAES(String key) throws NoSuchAlgorithmException {
if (null == key || key.length() == 0) {
throw new NullPointerException("key not is null");
}
SecretKeySpec key2 = null;
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(key.getBytes());
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(256, random); //这里到底是128还是256??但是两种试了还是报错pad block corrupted
//kgen.init(128, random);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
key2 = new SecretKeySpec(enCodeFormat, "AES");
} catch (NoSuchAlgorithmException ex) {
throw new NoSuchAlgorithmException();
}
return key2;
}
秘钥是一致的,这个AES-256-ECB解密(PKCS7Padding)到底怎么解啊,请问代码有问题吗
微信退款及退款通知结果 java_Java_leo-CSDN博客
https://blog.csdn.net/qq_35733535/article/details/83541574
要用SecretKeySpec key = new SecretKeySpec()这样的方法才可以
illegal base64 data at input byte 1110, 请问有人遇到这个问题吗?
https://developers.weixin.qq.com/community/develop/doc/00040e8a384050796f4835e4056400?_at=1587003251499
有java现成的可行的demo吗,跪求