Java AES/CFB解密
我正在尝试使用以下代码使用AES/CFB模式解密Java AES/CFB解密,java,encryption,aes,java-security,Java,Encryption,Aes,Java Security,我正在尝试使用以下代码使用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
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.
以上加密密钥和IVKEY均为有效密钥。有人能帮上忙吗?您正在呼叫的“ece7d4111337a51f81cbf2e3e42d105”
将导致大小为32的字节[]
,更不用说完全错误的IV了
您需要将字符串解析为字节[]
,例如,从javax.xml.bind
借用DatatypeConverter
IvParameterSpec iv = new IvParameterSpec(
javax.xml.bind.DatatypeConverter.parseHexBinary(initVector));
您正在调用“ece7d4111337a51f81cbf2e3e42d105”
将导致大小为32的字节[]
,更不用说完全错误的IV了
您需要将字符串解析为字节[]
,例如,从javax.xml.bind
借用DatatypeConverter
IvParameterSpec iv = new IvParameterSpec(
javax.xml.bind.DatatypeConverter.parseHexBinary(initVector));