Skip to content
Advertisement

IvParameterSpec value different for each print

When trying to understand a problem I faced an interesting problem. The IvParameterSpec class cannot hold the IV fixed. It changes for every println.

I am using the javac 10.0.2 version on Ubuntu Linux

import javax.crypto.spec.IvParameterSpec;
import java.util.Base64;

public class Encryptor {

    public static void main(String[] args) {

        IvParameterSpec ctr_iv;

        String IV = "0102030405060708";

        byte [] counter = IV.getBytes();

        ctr_iv = new IvParameterSpec(counter);
        System.out.println("ctr_iv = "+  ctr_iv.getIV());
        System.out.println("ctr_iv = "+  ctr_iv.getIV());
        System.out.println("ctr_iv = "+  ctr_iv.getIV());
        System.out.println("ctr_iv = "+  ctr_iv.getIV());
        System.out.println("ctr_iv = "+  ctr_iv.getIV());
        System.out.println("ctr_iv = "+  ctr_iv.getIV());
   }
}

Each of the println prints different values.

ctr_iv       = [B@42f30e0a
ctr_iv       = [B@24273305
ctr_iv       = [B@5b1d2887
ctr_iv       = [B@46f5f779
ctr_iv       = [B@1c2c22f3
ctr_iv       = [B@18e8568

What is the problem here? Shouldn’t the values be the same?

Advertisement

Answer

According to the documentation of the method IvParameterSpec.getIV():

Returns a new array each time this method is called.

So your assumption that the values should be the same might be true, but it is always a new array.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement