AES/CBC真的需要IV参数吗?

5 java aes password-encryption cbc-mode

我正在编写一个简单的应用程序来使用AES/CBC(模式)加密我的消息.由于我的理解CBC模式需要IV参数,但我不知道为什么我的代码工作没有使用IV参数.有谁能解释为什么?谢谢.

打印的加密消息:T9KdWxVZ5xStaisXn6llfg ==无例外.

public class TestAES {

public static void main(String[] args) {

    try {
        byte[] salt = new byte[8];
        new SecureRandom().nextBytes(salt);

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec keySpec = new PBEKeySpec("myPassword".toCharArray(), salt, 100, 128);

        SecretKey tmp = keyFactory.generateSecret(keySpec);
        SecretKeySpec key = new SecretKeySpec(tmp.getEncoded(), "AES");

        Cipher enCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        enCipher.init(Cipher.ENCRYPT_MODE, key);

        // enCipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));

        byte[] cipherBytes = enCipher.doFinal("myMessage".getBytes());
        String cipherMsg = BaseEncoding.base64().encode(cipherBytes);

        System.out.println("Encrypted message: " + cipherMsg);

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}
}
Run Code Online (Sandbox Code Playgroud)

bga*_*ath 6

当它在没有IV的情况下使用时,对于某些类型的密码,包括AES,它隐含地使用0 IV.请参阅密码类文档.

null IV(或确定性IV)的缺点是它易受字典攻击.IV的要求是防止同一个纯文本块每次产生相同的密文.

  • 实际上,如果您指定none,Java SE将使用随机IV.您的链接指向Java Card API.如果你没有指定一个你得到一个randome,你需要使用`cipher.getIV()`检索它,如果你没有设置它. (2认同)