我正在尝试使用AES/CFB模式通过以下代码解密,
final static public String ENCRYPT_KEY = "4EBB854BC67649A99376A7B90089CFF1";
final static public String IVKEY = "ECE7D4111337A511F81CBF2E3E42D105";
private static String deCrypt(String key, String initVector, String encrypted) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skSpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, skSpec, iv);
byte[] original = cipher.doFinal(encrypted.getBytes());
return new String(original);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}把错误抛在下面,
Wrong IV length: must be 16 bytes long.以上ENCRYPT_KEY和IVKEY是有效的。有人能帮上忙吗?
发布于 2016-11-28 11:16:59
您正在调用"ECE7D4111337A511F81CBF2E3E42D105".getBytes("UTF-8");,这将导致32大小的byte[],更不用说一个完全错误的IV了。
您需要将字符串解析为byte[],例如,从javax.xml.bind借用DatatypeConverter。
IvParameterSpec iv = new IvParameterSpec(
javax.xml.bind.DatatypeConverter.parseHexBinary(initVector));https://stackoverflow.com/questions/40842218
复制相似问题