这应该为您工作:
public String dec(String password, String salt, String encString) throws Exception { byte[] ivData = toByte(encString.substring(0, 32)); byte[] encData = toByte(encString.substring(32)); // get raw key from password and salt PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), toByte(salt), 50, 256); SecretKeyFactory keyFactory = SecretKeyFactory .getInstance("PBEWithSHA256And256BitAES-CBC-BC"); SecretKeySpec secretKey = new SecretKeySpec(keyFactory.generateSecret( pbeKeySpec).getEnpred(), "AES"); byte[] key = secretKey.getEnpred(); // setup cipher parameters with key and IV KeyParameter keyParam = new KeyParameter(key); CipherParameters params = new ParametersWithIV(keyParam, ivData); // setup AES cipher in CBC mode with PKCS7 padding BlockCipherPadding padding = new PKCS7Padding(); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESEngine()), padding); cipher.reset(); cipher.init(false, params); // create a temporary buffer to depre into (it'll include padding) byte[] buf = new byte[cipher.getOutputSize(encData.length)]; int len = cipher.processBytes(encData, 0, encData.length, buf, 0); len += cipher.doFinal(buf, len); // remove padding byte[] out = new byte[len]; System.arraycopy(buf, 0, out, 0, len); // return string representation of depred bytes return new String(out, "UTF-8");}我假设您实际上是在进行十六进制编码,
toByte()因为您的代码对IV使用32个字符(提供了必要的16个字节)。虽然我没有您用于加密的代码,但我确实验证了此代码将提供与您的代码相同的解密输出。



