想要使用带有32个字节的AES 256 CBC,但它显示了java.security.InvalidAlgorithmParameterException

时间:2012-11-20 05:54:29

标签: java security aes

我正在使用AES 256 CBC。我有32个字节的IV。但是当我运行它时,它会显示一个异常:

Exception in thread "main" java.lang.RuntimeException: java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long
    at com.abc.aes265cbc.AESUtil.decrypt(AESUtil.java:50)
    at com.abc.aes265cbc.Security.main(Security.java:48)
Caused by: java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long
    at com.sun.crypto.provider.CipherCore.init(CipherCore.java:430)
    at com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:217)
    at javax.crypto.Cipher.implInit(Cipher.java:790)
    at javax.crypto.Cipher.chooseProvider(Cipher.java:848)
    at javax.crypto.Cipher.init(Cipher.java:1347)
    at javax.crypto.Cipher.init(Cipher.java:1281)
    at com.abc.aes265cbc.AESUtil.decrypt(AESUtil.java:47)
    ... 1 more

我不知道如何解决这个问题。我搜索了但是我没有得到如何解决这个问题。我是第一次尝试安全概念。 我的AES 256 CBC代码是:

 public static void setENCRYPTION_IV(String ENCRYPTION_IV) {
        AESUtil.ENCRYPTION_IV  =   ENCRYPTION_IV;
    }

    public static void setENCRYPTION_KEY(String ENCRYPTION_KEY) {
        AESUtil.ENCRYPTION_KEY  =   ENCRYPTION_KEY;
    }



    public static String encrypt(String src) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, makeKey(), makeIv());
            return Base64.encodeBytes(cipher.doFinal(src.getBytes()));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String decrypt(String src) {
        String decrypted = "";
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, makeKey(), makeIv());
            decrypted = new String(cipher.doFinal(Base64.decode(src)));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return decrypted;
    }

    static AlgorithmParameterSpec makeIv() {
        try {
            return new IvParameterSpec(ENCRYPTION_IV.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    static Key makeKey() {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            byte[] key = md.digest(ENCRYPTION_KEY.getBytes("UTF-8"));
            return new SecretKeySpec(key, "AES");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return null;
    }

你能帮助我吗?通过改变这段代码中的内容,我将能够使用32字节的IV。 提前致谢

编辑: 我调用它的主要功能是:

 AESUtil.setENCRYPTION_KEY("96161d7958c29a943a6537901ff0e913efaad15bd5e7c566f047412179504ffb");

    AESUtil.setENCRYPTION_IV("d41361ed2399251f535e65f84a8f1c57");
    String decrypted = AESUtil.decrypt(new String(sw0SrUIKe0DmS7sRd9+XMgtYg+BUiAfiOsdMw/Lo2RA=));   // AES Decrypt

1 个答案:

答案 0 :(得分:9)

AES算法具有128位块大小,无论您的密钥长度是256,192还是128位。

当对称密码模式需要IV时,IV的长度必须等于密码的块大小。因此,您必须始终使用带有AES的128位(16字节)的IV。

无法使用带有AES的32字节IV。