8

Trying to convert a byte[] to base64 string using org.apache.commons.codec.binary.Base64..For this my java code looks like:

 base64String = Base64.encodeBase64URLSafeString(myByteArray);

But what i see is some invalid characters in the generated base64 string..ScreenShot

Why do I see these ____ lines in my generated base64 String? Is it a valid string? Note the length of the generated string is dividable by four.

5
  • 1
    did you try decoding your output? Also, are you using the encoded base64 data in an URL. Use the URLSafe version only when the base64 encoding goes into a http/get or some other url-related operation. Commented Jul 23, 2012 at 12:48
  • en.wikipedia.org/wiki/Base64#URL_applications Commented Jul 23, 2012 at 12:48
  • This might help stackoverflow.com/questions/6265912/… Commented Jul 23, 2012 at 12:48
  • Please provide the code you have written Commented Jul 23, 2012 at 12:52
  • @developer : All I use to convert the byte[] to string is a single methid which i have already provided in my question... Commented Jul 23, 2012 at 13:06

5 Answers 5

3

have you tried with the encodeBase64String method instead of using encodeBase64URLSafeString ?

encodeBase64URLSafeString:

Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The url-safe variation emits - and _ instead of + and / characters.

encodeBase64String:

Encodes binary data using the base64 algorithm but does not chunk the output. NOTE: We changed the behaviour of this method from multi-line chunking (commons-codec-1.4) to single-line non-chunking (commons-codec-1.5).

source : org.apache.commons.codec.binary.Base64 javadoc

Sign up to request clarification or add additional context in comments.

Comments

2

Use

String sCert = javax.xml.bind.DatatypeConverter.printBase64Binary(pk); 

Comments

1

This may be helpful

sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
encoder.encode(byteArray);

2 Comments

This is bad advice---sun.misc package hierarchy is not for public use.
I dont want to go with sun.misc
0

you've got (at least) two flavours of base64, the original one using '+' and '/' in addition to alphanumeric characters, and the "url safe" one, using "-" and "_" so that the content can be enclosed in a URL (or used as a filename, btw).

It looks like you're using a base64 encoder that has been turned into "url-safe mode".

apache's javadoc for URLSafeString()

Oh, and '_' being the last character of the base64 alphabet, seeing strings of "____" means you've been encoding chunks of 0xffffff ... , just like seeing "AAAAAA" means there's a lot of consecutive zeroes.

If you want to be convinced that it's a normal case, just pick your favourite hexadecimal dumper/editor and check what your binary input looked like.

2 Comments

Looks a valid comment amongst all yes I have got / and replcaed the econding method with encodeBase64URLSafeString to avoid slashes.. Then it all started with _.. All i need to know is the above is a valid base64 string ?Kindly suggest.
you're asking if the string matches [0-9A-Za-z_-]+ or [0-9A-Za-z/+]+ ? that sounds like a job for a regexp.
-1

By the below process you can convert byte array to Base64

 // Convert a byte array to base64 string
    byte[] bytearray= new byte[]{0x12, 0x23};
    String s = new sun.misc.BASE64Encoder().encode(bytearray);

    // Convert base64 string to a byte array
    bytearray = new sun.misc.BASE64Decoder().decodeBuffer(s);

Edit:

Please check for guide below link commons apache

2 Comments

` sun.misc.BASE64Decoder()` ?? Why am trying with apache commons
sun.misc is something internal to Sun's implementation of java/javax packages. There is no guarantee it will always be there (esp. on a different JVM) nor that its interface will remain the same across different releases of the JVM, afaik.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.