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.

Suave Nti
  • 3,721
  • 11
  • 54
  • 78
  • 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. – maasg Jul 23 '12 at 12:48
  • http://en.wikipedia.org/wiki/Base64#URL_applications – nullpotent Jul 23 '12 at 12:48
  • This might help http://stackoverflow.com/questions/6265912/javax-xml-binds-base64-encoder-decoder-eats-last-two-characters-of-string – tsm Jul 23 '12 at 12:48
  • Please provide the code you have written – developer Jul 23 '12 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... – Suave Nti Jul 23 '12 at 13:06

5 Answers5

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

Jean-Philippe Caruana
  • 2,617
  • 4
  • 25
  • 47
2

Use

String sCert = javax.xml.bind.DatatypeConverter.printBase64Binary(pk); 
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Taran
  • 2,895
  • 25
  • 22
1

This may be helpful

sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
encoder.encode(byteArray);
Koti
  • 131
  • 4
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.

PypeBros
  • 2,607
  • 24
  • 37
  • 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. – Suave Nti Jul 23 '12 at 13:04
  • you're asking if the string matches [0-9A-Za-z_-]+ or [0-9A-Za-z/+]+ ? that sounds like a job for a regexp. – PypeBros Jul 24 '12 at 08:57
-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

developer
  • 9,116
  • 29
  • 91
  • 150
  • ` sun.misc.BASE64Decoder()` ?? Why am trying with apache commons – Suave Nti Jul 23 '12 at 12:59
  • 3
    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. – PypeBros Jul 24 '12 at 09:02