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.