IvParameterSpec value different for each print












1















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?










share|improve this question




















  • 1





    You would expect IvParameterSpec to behave like an immutable class. The only way to do so is to return a copy of its underlying byte array.

    – James K Polk
    Nov 24 '18 at 16:55
















1















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?










share|improve this question




















  • 1





    You would expect IvParameterSpec to behave like an immutable class. The only way to do so is to return a copy of its underlying byte array.

    – James K Polk
    Nov 24 '18 at 16:55














1












1








1








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?










share|improve this question
















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?







java linux encryption






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 14:15









Ishaan Javali

1,3493820




1,3493820










asked Nov 24 '18 at 14:13









kelalakakelalaka

1,52121124




1,52121124








  • 1





    You would expect IvParameterSpec to behave like an immutable class. The only way to do so is to return a copy of its underlying byte array.

    – James K Polk
    Nov 24 '18 at 16:55














  • 1





    You would expect IvParameterSpec to behave like an immutable class. The only way to do so is to return a copy of its underlying byte array.

    – James K Polk
    Nov 24 '18 at 16:55








1




1





You would expect IvParameterSpec to behave like an immutable class. The only way to do so is to return a copy of its underlying byte array.

– James K Polk
Nov 24 '18 at 16:55





You would expect IvParameterSpec to behave like an immutable class. The only way to do so is to return a copy of its underlying byte array.

– James K Polk
Nov 24 '18 at 16:55












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459039%2fivparameterspec-value-different-for-each-print%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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.






    share|improve this answer




























      1














      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.






      share|improve this answer


























        1












        1








        1







        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.






        share|improve this 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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 14:18









        SeelenvirtuoseSeelenvirtuose

        16.7k42848




        16.7k42848
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459039%2fivparameterspec-value-different-for-each-print%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin