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

  • Home
  • SEARCH
  • 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

There are similar question, but not regarding C# libraries I can use in my
Similar question as this one but for a Microsoft Environment. Email --> Exchange Server
A similar question discusses __construct , but I left it in my title for
A similar question was already asked ( Performing a Stress Test on Web Application?
A similar question has been asked: MSDN subscriptions on the cheap? , but I
This question was inspired by a similar question: How does delete[] know the size
I've found a similar question on stack overflow, but it didn't really answer the
I posted a similar question on how scalable linq is. There were so many
There is a similar question going around, but it just got the same old
I've seen similar questions and answers regarding conversions from virtual to absolute and url,

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.