Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7506473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:16:19+00:00 2026-05-29T22:16:19+00:00

I need to convert a salt value randomly generated and store it in the

  • 0

I need to convert a salt value randomly generated and store it in the database. To store it in the database I converted it to a string. Then for retrieving the original value, I convert it back to byte. But both value are not matching. I have tried “UTF-8″,”UTF-16”, BASE64Encoder.

SecureRandom ranGen = new SecureRandom();
byte[] aesKey = new byte[16]; // 16 bytes = 128 bits
ranGen.nextBytes(aesKey);   
System.out.println(aesKey);

String a=new String(aesKey,"UTF-16");
byte[] b=new byte[16];
b=a.getBytes("UTF-16");
System.out.println(b);

Outputs for the above code(Executed it 2 times):

[B@11563ff
[B@1581593

and

[B@170888e
[B@11563ff
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:16:22+00:00Added an answer on May 29, 2026 at 10:16 pm

    As your code is written above, printing aesKey and then b, what you are actually printing is the output of the toString method for an array object, which is just the default Object toString method. So I don’t see how you can expect them to be the same.

    If you really want to check they are the same you should compare them byte by byte.

    In terms of your actual question regarding storing a byte[] as a String in the DB, your best bet is to Base64 encode it. I would suggest using the Apache Commons Codec library for this. See the user guide.

    EDIT:

    Using the BASE64Encode and BASE64Decoder you have referred to, the code would be like this:

        SecureRandom ranGen = new SecureRandom();
        byte[] aesKey = new byte[16]; // 16 bytes = 128 bits
        ranGen.nextBytes(aesKey);
        String a = new BASE64Encoder().encode(aesKey);
    
        System.out.println(a);
    
        byte[] b = new BASE64Decoder().decodeBuffer(a);
        System.out.println(new BASE64Encoder().encode(b));
    
        for (int i = 0; i < aesKey.length; i++) {
            System.out.println(aesKey[i] + " " + b[i]);
        }
    

    Here, I have also looped through the bytes individually, to show that they are indeed equal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to convert byte arrays of images and videos back to its origin. I
I need to convert a value which is in a DateTime variable into a
I need to convert from a SQLVARCHAR to a string data type Variable definitions
Need to convert a string to normal datetime format so it can be correctly
I need convert some Mac Numbers(00163e2fbab7) into Mac String. (with :) Is there some
Equivalent to utf8_decode in php I have an string: tópicos I need convert to
I need convert a String to a decimal in C#, but this string have
I create a string: myOption = "{val1:'text1', val2:'text2'...}" I need convert this string into
I have a string and need convert it to a date, but it doesn't
I need to convert a string to URI, the string is a link to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.