0

I am trying to decode a base64 encoded string. I wrote the following piece of code for that:

String bytesEncoded = "rO0ABXNyADZ6YS5jby5zYi5wYXltZW50cy50by5pbnN0cnVjdGlvbi5CYXRjaEZpbGVVcGxvYWRD"
+ "UlVEVE8dnJ9z1jdsQwIAB0wAEGFic29sdXRlUGF0aE5hbWV0ABJMamF2YS9sYW5nL1N0cmluZztM"
+ "AAliYXRjaFR5cGVxAH4AAUwAFmN1c3RvbWVyUGF5bWVudFR5cGVLZXl0ABBMamF2YS9sYW5nL0xv"
+ "bmc7TAAhZW5jb2RlZEJ5dGVTdHJlYW1QYXltZW50QmF0Y2hGaWxlcQB+AAFMAAhmaWxlTmFtZXEA"
+ "fgABTAAIZmlsZVR5cGVxAH4AAUwABmlzc3Vlc3QAJEx6YS9jby9zYi9jb3JlL2NvbW1vbi90by9J"
+ "c3N1ZUxvZ1RPO3hyAB96YS5jby5zYi5jb3JlLnRvLkFic3RyYWN0Q1JVRFRP9ka5cD8D+JUCAAJK"
+ "AA12ZXJzaW9uTnVtYmVyTAAGYWN0aW9ucQB+AAF4cP//////////dAAGdXBsb2FkdACYQzpcbkJv"
+ "bF9Mb2FkVGVzdGluZ1xQZXJmb3JtYW5jZVRlc3RpbmdcbkJPTFxDVkFcUmVsZWFzZTE1XE1pc2Mg"
+ "RG9jc1xGaWxlcyBmb3IgVXBsb2FkXG5Cb2wgUHJvcCBSMTMtIERvbWVzdGljIGFkaG9jIDA4MDgy"
+ "MDE0IFRhbnphbmlhQmVuMiBWYWxpZCBQZXJmMi54bWx0AAdQYXltZW50c3IADmphdmEubGFuZy5M"
+ "b25nO4vkkMyPI98CAAFKAAV2YWx1ZXhyABBqYXZhLmxhbmcuTnVtYmVyhqyVHQuU4IsCAAB4cAAA"
+ "AAAAJKWidANsSDRzSUFBQUFBQUFBQU1WVlhXL1RNQlI5UitJL1JIMHZUanNZcmVSbFN0TU5LcUZS"
+ "MnNJUWIzZk9YV3Nwc1lmamxKVmZqeE5ucVowR05DUWtwS3J4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1B4ZFU2YjJldWpvMGV2NENlaS9h"
+ "YW1ubUw4cTAyOHp5R3hIOXBmc2pQKzlLZ0hBQUE9dABDbkJvbCBQcm9wIFIxMy0gRG9tZXN0aWMg"
+ "YWRob2MgMDgwODIwMTQgVGFuemFuaWFCZW4yIFZhbGlkIFBlcmYyLnhtbHBw";

byte[] decodedBytes = Base64.decodeBase64(bytesEncoded);
System.out.println("decodedBytes " + new String(decodedBytes));

The output that i get for this shows as below:

System.out: decodedBytes ’

Now my issue is, i want to know if this is due to variable length. If yes is there a way to increase the length?

I am new to java and did this by searching from internet. Please ignore if i am being very naive.

I tried to just convert a part of the encoded string (just the last line. shown below) and it showed me the string properly.

String bytesEncoded = "YWRob2MgMDgwODIwMTQgVGFuemFuaWFCZW4yIFZhbGlkIFBlcmYyLnhtbHBw";

byte[] decodedBytes = Base64.decodeBase64(bytesEncoded);
System.out.println("decodedBytes " + new String(decodedBytes));

Output for the same below:

System.out: decodedBytes adhoc 08082014 TanzaniaBen2 Valid Perf2.xmlpp
icza
  • 389,944
  • 63
  • 907
  • 827
MitSM
  • 21
  • 11
  • what is the original text ? – Mifmif Aug 13 '14 at 08:55
  • The original text is the one entered first. That is to be decoded – MitSM Aug 13 '14 at 09:02
  • If I use your base64-string via an online [base64-encoder/decoder](http://www.motobit.com/util/base64-decoder-encoder.asp) it seems that your are trying to fill the bytes of a serialized object of class za.co.sb.payments.to.instruction.BatchFileUploadCRUDTO to a String – Roman Vottner Aug 13 '14 at 09:04
  • thats correct. But i dont get the full string – MitSM Aug 13 '14 at 09:20
  • I just copy&pasted your code snippet to a new Eclipse project and used Apaches common-codecs 1.9 Base64 class and got the complete expected output as with the online base64-encoder/decoder – Roman Vottner Aug 13 '14 at 09:22
  • i have imported import org.apache.commons.codec.binary.Base64; – MitSM Aug 13 '14 at 09:28
  • can you paste the exact code that you used in Eclipse? Nothing changed. not even the spaces – MitSM Aug 13 '14 at 09:29
  • I just copy&pasted your code 1:1 and put it inside a `static void main(String ... args)` method and included the same import as you did. Which version of java are you running? I currently use Oracle/Sun's 64bit 1.7.0_60 - btw. I'm on Mac 10.9.4 – Roman Vottner Aug 13 '14 at 09:33
  • i am running on 1.7.0_45 and i have a 32 bit OS – MitSM Aug 13 '14 at 09:36
  • have you declared the String bytesEncoded the same way i did? I mean with all the pluses? – MitSM Aug 13 '14 at 09:37
  • by copying something 1:1 I do not make [any modifications](https://imagizer.imageshack.us/v2/2880x1748q50/674/ho3aiU.png) – Roman Vottner Aug 13 '14 at 09:46

4 Answers4

0

The decoded bytes simply don't denote a string in the default character encoding (which may vary from platform to platform).

It could be it doesn't even denote a string at all (which seems likely in your case). But if so, you should always specify the encoding explicitly like this:

String s = new String(bytesDecoded, charsetName);
// or
String s = new String(bytesDecoded, charset);

The constructor of String can take any length of a byte array, that is not the problem.

Here's an example that charset matters:

String s = "Hi éáű!";
Systme.out.println(s); // Prints "Hi éáű!" obviously

byte[] b = s.getBytes(StandardCharsets.UTF_8);

// Next line also prints "Hi éáű!", charsets match:
System.out.println(new String(b, StandardCharsets.UTF_8));

// Next line prints "Hi éáű!", decoded with a different charset!
System.out.println(new String(b, StandardCharsets.ISO_8859_1));

Also note that the String constructor does not interpret BOM sequences (e.g. if your decoded byte array starts with a byte order mark sequence, it will not be processed properly but interpreted as bytes of the string).

icza
  • 389,944
  • 63
  • 907
  • 827
0

The byte block you are trying to convert is definitely not a string of text, but binary data of some kind.

It contains multiple 0-valued bytes, which usually denote a string terminator and that's the reason why your original code only shows two characters.

  • Java doesn't use C style ('\0' char terminated) strings or byte sequences. – icza Aug 13 '14 at 09:17
  • I think that might be a reason for this. But then i tried putting the whole sequence in a single line. It still shows the same thing. – MitSM Aug 13 '14 at 09:30
  • Java as a language does not use string terminators, but a string with '\0' will be cut when you output it on most console viewers. – jguillo Dec 16 '14 at 07:14
0

Use apache commons Codec. It has a Base64 class.

use it like so:

import org.apache.commons.codec.binary.Base64;
[..]
byte[] decoded = Base64.decodeBase64(base64string);

Maven artifact:

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.9</version>
</dependency>
Benjamin Marwell
  • 1,173
  • 1
  • 13
  • 36
  • Based on the other answers, it's directly decoded to a byte array. You can than check if it contains useful text information. – Benjamin Marwell Aug 13 '14 at 09:13
  • Then it is binary data. If you are sure it is not, you can still try to force-encode it via: [Commons Codec StringUtils](http://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/StringUtils.html). – Benjamin Marwell Aug 13 '14 at 10:30
0

Since the decoded bytes actually contain a binary file (with small text parts), you probably do not mean to treat the decoded bytes as encoded text characters, which is what String(byte[]) will do for you.

Instead you may want to print only the printable characters (by printing out the bytes char-by-char, and testing each char to see if it's displayable), like described in this topic or by displaying it as a string of hexadecimal characters, like described in this topic

Community
  • 1
  • 1