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 811753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:05:40+00:00 2026-05-15T01:05:40+00:00

In the similar question Conversion of byte[] into a String and then back to

  • 0

In the similar question “Conversion of byte[] into a String and then back to a byte[]” is said to not to do the byte[] to String and back conversion, what looks like apply to most cases, mainly when you don’t know the encoding used.

But, in my case I’m trying to save to a DB the javax.crypto.SecretKey data, and recoverd it after.

The interface provide a method getEncoded() which returns the key data encoded as byte[], and with another class I can use this byte[] to recover the key.

So, the question is, how do I write the key bytes as String, and later get back the byte[] to regenerate the key?

  • 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-15T01:05:40+00:00Added an answer on May 15, 2026 at 1:05 am

    javax.crypto.SecretKey is binary data, so you can’t convert it directly to a String. You can encode it as a hex string or in Base64.

    See Apache Commons Codec.

    Update: If you dont want to depend on third-party libraries (and can’t/don’t want to store plain binary data, as Jon suggests) you can do some ad-hoc encoding, for example, following erickson’s suggestion:

    public static String bytesToString(byte[] b) {
        byte[] b2 = new byte[b.length + 1];
        b2[0] = 1;
        System.arraycopy(b, 0, b2, 1, b.length);
        return new BigInteger(b2).toString(36);
    }
    
    public static byte[] stringToBytes(String s) {
        byte[] b2 = new BigInteger(s, 36).toByteArray();
        return Arrays.copyOfRange(b2, 1, b2.length);
    }
    

    It’s rather, ugly, non-standard and not optimal (in output size). But it’s also very small, correct and it has no dependencies; it can be practical, specially if your data is small sized.

    Updated: I replaced the Character.MAX_RADIX by the literal value (36), following GregS’s comment. It might seem less elegant but it’s actually more secure. (You can also use 32 or 16).

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

Sidebar

Related Questions

A similar question was asked here: PHP variable like $myvar-test is not valid? And
Similar question , but for Oracle. Why would I not always want to choose
A similar question Long in Float, why? here does not answer what I am
I have checked this similar question, but the suggestions did not solve my problem:
Similar question: How to mimic UITableView's UITableViewStylePlain section header style No complete answers. BarrettJ's
Similar question: Best Resources for Learning AS3 I am interested in learning AS3 and
A similar question was asked here Serving images from outside of document root The
A similar question was asked here but without a satisfying answer. I'm trying to
A similar question was asked about a year ago, and wasn't quite resolved, but
My question popped up a very similar question, this one . But the accepted

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.