from what I know, CTR mode does not use the start vector. It just takes the counter, encrypts it with the given key, and then XOR returns the result in clear text to get the encrypted text.
Other block encryption modes, such as CBC, before performing encryption, they are XOR plaintext with an initial vector.
So here is my problem. I have the following Java code (using the bouncycastle library):
Cipher cipher = Cipher.getInstance("AES/CTR/PKCS5Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] result = cipher.doFinal("Some plaintext");
Each different call to the above code with the same key gives an excellent result! But on execution:
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");
I accept the same result in every call to the above code. But why is that? I mean, CTR does not need an IV, so why, when I do not give IV in each call, I get a different result, and when I give IV, it returns the same result? If I always use the above IV (all zeros) when using CTR, will it be safe?
Any ideas would be very helpful. Thanks you