Tag: initialization vector

CTR模式使用初始向量(IV)

据我所知,CTR模式不使用初始向量。 它只需要一个计数器,用给定的密钥对其进行加密,然后用明文对结果进行异或,以获得密文。 其他分组密码模式,如CBC,在进行加密之前,它们使用初始向量对明文进行异或。 所以这是我的问题。 我在Java中使用以下代码(使用bouncycastle库): Cipher cipher = Cipher.getInstance(“AES/CTR/PKCS5Padding”, “BC”); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] result = cipher.doFinal(“Some plaintext”); 上述代码的每个不同调用使用相同的键给出不同的输出! 但是在做的时候: byte[] IV = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; Cipher cipher = Cipher.getInstance(“AES/CTR/PKCS5Padding”, “BC”); cipher.init(Cipher.ENCRYPT_MODE, key, IV); byte[] result = cipher.doFinal(“Some plaintext”); 我在上述代码的每次调用中都会得到相同的结果。 但为什么会这样呢? 我的意思是,CTR不需要IV,那么为什么当我不在每次通话中给出IV时我得到不同的结果,当我给出IV时它会返回相同的结果? […]

在Java中为AES生成随机IV

我正在为Android中的PBE实现和AES加密引擎,我发现了两种方法来实现IV的创建,我想知道哪个更好,更安全,以获得IvParameterSpec : 方法#1: SecureRandom randomSecureRandom = SecureRandom.getInstance(“SHA1PRNG”); byte[] iv = new byte[cipher.getBlockSize()]; randomSecureRandom.nextBytes(iv); IvParameterSpec ivParams = new IvParameterSpec(iv); 方法#2: AlgorithmParameters params = cipher.getParameters(); byte[] iv2 = params.getParameterSpec(IvParameterSpec.class).getIV(); ivParams = new IvParameterSpec(iv2);

需要解决AES中错误的IV长度问题

我正在尝试在Java中实现AES,这是我使用的代码: byte[] sessionKey = {00000000000000000000000000000000}; byte[] iv = {00000000000000000000000000000000}; byte[] plaintext = “6a84867cd77e12ad07ea1be895c53fa3”.getBytes(); Cipher cipher = Cipher.getInstance(“AES/CBC/PKCS5Padding”); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sessionKey, “AES”), new IvParameterSpec(iv)); byte[] ciphertext = cipher.doFinal(plaintext); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(sessionKey, “AES”), new IvParameterSpec(iv)); byte[] deciphertext = cipher.doFinal(ciphertext); 我需要这个固定密钥和IV用于测试目的,但我得到以下exception: Exception in thread “main” java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long at com.sun.crypto.provider.SunJCE_h.a(DashoA12275) at com.sun.crypto.provider.AESCipher.engineInit(DashoA12275) […]