【问题标题】:AES/CFB decryptionAES/CFB 解密
【发布时间】:2017-04-12 01:15:20
【问题描述】:

我正在尝试使用以下代码使用 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 以上都是有效的。有人可以帮忙吗?

【问题讨论】:

    标签: java encryption aes java-security


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 2021-12-20
      • 2017-02-21
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      相关资源
      最近更新 更多