Byte [] != Byte []

What is this all about...

This code:

    String str = new String( "HI!" );
    byte [] b1 = str.getBytes( "UTF8" );
    byte [] b2 = str.getBytes( "UTF8" );
    System.out.println( b1.equals(b2) );



But why? Can I compare two arrays without having to compare each individual element?
LVL 25
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

b1 & b2 are two separate objects.. i.e. if you try to == it will come out as false...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
InteractiveMindAuthor Commented:
Okay, fair enough. But, would you agree that the *content* of the two byte arrays in my above example would be equivalent? (I think we all know that they are), in which case, why do I get errors when I do the following:

    String sPT = new String( "Experts Exchange" );
    byte [] bPT = sPT.getBytes( "UTF8" );
    byte [] bCT = encrypt( bPT );
    String sCT = new String( bCT, "UTF8" );
    bCT = sCT.getBytes( "UTF8" );            // With this line
    bPT = decrypt( bCT );
    sPT = new String( bPT, "UTF8" );

Than when I do this:

    String sPT = new String( "Experts Exchange" );
    byte [] bPT = sPT.getBytes( "UTF8" );
    byte [] bCT = encrypt( bPT );
    String sCT = new String( bCT, "UTF8" );
    bPT = decrypt( bCT );
    sPT = new String( bPT, "UTF8" );


Does that make sense to you?
InteractiveMindAuthor Commented:

    byte [] bCT = encrypt( bPT );
    String sCT = new String( bCT, "UTF8" );
    bCT = sCT.getBytes( "UTF8" );

causes problems, but it's fine when I don't use that last line... surely it should produce the same result as the first line?!
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

and your encypted byte array and the byte that you get out of the encrypted strings are not  matching......right?
InteractiveMindAuthor Commented:
Ah ha... nevermind about that one; I've figured it .... there was a bug in my decrypt() method; I wasn't using UTF8 how I should have been.


Thanks all the same.
then I dont really deserve those points...:-)
What was the problem by the way if you dont mind sharing
InteractiveMindAuthor Commented:
Well, you answered my original question; so, I think the points should be yours..

I'd setup 2 methods, one to encrypt a byte array, the other to decrypt..

When I retrieved the ciphertext version of the data to be encrypted, like so:
   byte [] b = encrypt(); // Obviously, I'd pass the required parameters to this method
I then binded the byte array to a string like so:

   String s = new String( b, "UTF8" );
Then, if I wanted to decrypt 's', rather than using 'b', I done the following:

   byte [] b2 = s.getBytes( "UTF8" );
Expecting b2 to be the same as b.. They weren't however, which was resulting in an exception when I attempted to re-encrypt..

What was causing 'b' != 'b2', was that in the decrypt method, I wasn't converting the data into UTF8 when I needed to....... But that's now fixed. :-)
Use Arrays.equals(b1,b2) to compare arrays by content.
InteractiveMindAuthor Commented:
Oh yeah! Thanks for that dberner9 :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.