java - AES/CFB解密

标签 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) {
    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是有效的。有人可以帮忙吗?

最佳答案

您正在调用 "ECE7D4111337A511F81CBF2E3E42D105".getBytes("UTF-8"); 这将导致 byte[] 大小为 32,更不用说完全错误的 IV。

您需要将字符串解析byte[],例如通过从javax.xml借用DatatypeConverter .bind.

IvParameterSpec iv = new IvParameterSpec(
    javax.xml.bind.DatatypeConverter.parseHexBinary(initVector));

关于java - AES/CFB解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40842218/

相关文章:

php - 无需加密即可保护 PHP 代码的最佳解决方案

ruby - Ruby 中的单 block AES 解密

java - 在 .exe 中打包时出现 AES 256 加密/解密 NoSuchProviderException(使用 Advanced Installer 9.4)

mysql - 是否可以加密 mysql 数据库中表的列

python - AES key 移入配置文件生成 "AES key must be either 16, 24, or 32 bytes long"错误

Aes key 长度的意义/含义

java - Apache axis2 wsdl2java异常: unknown protocol: d

java - 使用 Java 创建 Adob​​e XD 背景模糊效果

java - 最小化 LWJGL 中的显示

Java 说 XML 文档格式不正确