有java现成的可行的demo吗,跪求
退款结果通知解密req_info失败,麻烦看看代码有错误吗?/** * 对微信的返回数据进行解密 做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)到底怎么解啊,请问代码有问题吗
2020-04-15