1

EDIT: For those asking to see the entire methods/classes

I have a class Encrypter which creates an IVParameterSpec in a constructor, encodes the IVParameterSpec in the Encoder method and returns the IVParameterSpec in the GetIV method.

This is the Encrypter class

public class Encrypter
{

   public IVParameterSpec ctr_iv;   

   public Encrypter(int keylength)
    {
        //ctr_iv is created in this constructor
        byte [] counter = new byte[16];
        ctr_iv = new IvParameterSpec(counter);
        System.out.println("The iv is " + ctr_iv);
    }

   public String Encoder()
    {
        String encoded_IV = Base64.getEncoder().encodeToString(ctr_iv.getIV());

        return encoded_IV;
    }

   public IvParameterSpec getIV()
    {
        return ctr_iv;
    }
}

I have a class Decrypter that decodes the IVParameterSpec in a constructor and returns the IVParameterSpec in the GetIV method.

public class Decrypter
{
   IvParameterSpec retrieved_iv;

    public Decrypter(String iv)
    {
        byte [] decodedIV = Base64.getDecoder().decode(iv);
        retrieved_iv = new IvParameterSpec(decodedIV);
        System.out.println("The iv in this class is " + retrieved_iv);
    }

   public IvParameterSpec getIV()
    {
        return retrieved_iv;
    }
}

My aim is to make sure that retrieved_iv in the Decryptor class is equal to the ctr_iv in the Encrypter class. I initially thought the constructors above would do the trick. But when I tested their values for equality, I found out that they were not equal. This is how I tested them:

public class Main
{
   public static void main(String [] args)
    {
        Encrypter encrypter = new Encrypter();  //Initializes ctr_iv
        Decrypter decrypter = new Decrypter(encrypter.Encoder()); //Encodes ctr_iv in first object and passes it as an argument to the constructor of the 2nd object

        if(encrypter.GetIV().equals(decrypter.GetIV()))
        {
            System.out.println("IV's are equal");
        }else{System.out.println("IV's are not equal");}
    }
}

I have a feeling this has something to do with how I'm decoding the string in the Decrypter constructor.

Vktr
  • 79
  • 6

1 Answers1

3

Compare by Arrays.equals.

if(Arrays.equals(ctr_iv.getIV(),retrieved_iv.getIV()))         {
    System.out.println("IV's are equal");
} else {
    System.out.println("IV's are not equal");
}
kelalaka
  • 4,046
  • 4
  • 22
  • 39
  • The Encoder and Decoder methods are in separate classes (sorry i didn't mention this earlier. I'll edit the description). So I don't think this would work. – Vktr Nov 23 '18 at 20:22
  • @Vktr when you print the values aren't they same? – kelalaka Nov 23 '18 at 20:24
  • Exactly. I even used a conditional statement that checks if they're equal. If they are equal, print "Pass", otherwise print "Fail". But it prints Fail every time. – Vktr Nov 23 '18 at 20:28
  • @Vktr how you compare? how you transfer the IV between the two classes? – kelalaka Nov 23 '18 at 20:30
  • The Encoder method is in a class called Encrypter and the Decoder method is in a class called Decrypter. In the Encrypter class, I created a method called GetIV(), which simply returns ctr_iv and in the Decrypter class, I have a method called GetIV() which simply returns retrieved_iv. In the Main class, I created objects for both Encryptor (encrypterobject) and Decryptor (decrypterobject). Then I called the Encoder method to encrypt the IV and then I called the Decoder method, where I passed encrypterobject.Encoder() as the string argument. – Vktr Nov 23 '18 at 20:37
  • And then I called the GetIV methods for both objects and conpared their values. They were different. – Vktr Nov 23 '18 at 20:39
  • @Vktr you should definitely post the main function, getIV function etc.. – kelalaka Nov 23 '18 at 20:42
  • I edit the question and added all the methods you need to see. – Vktr Nov 24 '18 at 12:39
  • So you're implying that my implementation of decoding the string to retrieve the original iv is correct? – Vktr Nov 25 '18 at 05:51
  • As I can see, there is nothing wrong in base64 decode and encode operations, I run a small test within a single class. Your problem is comparing the byte[] which must be with Arrays.equals. – kelalaka Nov 25 '18 at 07:18
  • But I'm comparing the IVParameterSpec values. Not the byte arrays. Shouldn't I use "equals" in this case? I remember when I was comparing two SecretKey values earlier. I had to use "equals" for that. So if I'm comparing two IVParameterSpec values, shouldn't it follow the same logic? – Vktr Nov 25 '18 at 09:38
  • 1. This what we expect with the new operator creates a new object. If the values in the byte[] are same the will two different IVParameterSpec object that is carrying the same IV's. 2. You cannot carry one object from another class, you can only carry the values and then create a new object with this values. And remeber, even for functions java is [Is Java “pass-by-reference” or “pass-by-value”?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – kelalaka Nov 25 '18 at 11:14
  • My apologies. You were right. Even though the printouts for the IVParameterSpec values were different, they both had the same byte array. – Vktr Nov 25 '18 at 11:46