13

I have to decode a base64 encoded data received from a PHP server.

The server uses 'base64_encode' to encode the data.

In my Android app, I use android.utils.Base64 class to do the decoding.

original encrypted data = "†+Ü]M(‡=ñö"
Base64 encoding data in PHP gives - "hisP3F1NBCgIAocQCD3x9g=="
Base64 encoding data in Android gives - "4oCgKw/DnF1NBCgIAuKAoRAIPcOxw7Y="

As you can see, the Java encoded string is longer than the PHP encoded string. I need to find out their default encoding formats.

How to get the same encoded string from both?

Java/Android code :

String encrypted = "†+Ü]M(‡=ñö";
byte[] encoded = Base64.encode(encrypted.getBytes(), Base64.DEFAULT);
String str = new String(encoded); //str = "4oCgKw/DnF1NBCgIAuKAoRAIPcOxw7Y="  
2
  • 1
    encrypted.getBytes() depends on the locale encoding. most likely utf-8 in your case. in php, it will depend on the server, likely a fixed size 8-bit encoding. the bytes you give to your base64 encoder are different. Commented Mar 1, 2013 at 11:45
  • @userSeven7s: the output from the PHP is based on CP1252, the output from JAVA is based on UTF-8, I'll update my answer below Commented Mar 1, 2013 at 11:56

3 Answers 3

15

Try this in Java: This will give you the long version of the string (UTF-8)

byte[] encoded = Base64.encode(encrypted.getBytes("UTF-8"), Base64.DEFAULT);
String str = new String(encoded, "UTF-8");

Updated:

Try this in Java: This will give you the short version of the string (CP1252)

// This should give the same results as in PHP
byte[] encoded = Base64.encode(encrypted.getBytes("CP1252"), Base64.DEFAULT);
String str = new String(encoded, "CP1252");

Alternatively try this PHP Script:

file: test.php

<?php

echo base64_encode($_GET['str'])." Default UTF-8 version<br />";
echo base64_encode(iconv("UTF-8","CP1252",$_GET['str']))." CP1252 Version <br />";

?>

usage: http://[SOMEDOMAIN]/test.php?str=†+Ü]M(‡=ñö
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks dude.. That was a big help.. Appreciate it. "CP1252" did the trick. I was a google search away from the answer.
how can I decode this // This should give the same results as in PHP byte[] encoded = Base64.encode(encrypted.getBytes("CP1252"), Base64.DEFAULT); String str = new String(encoded, "CP1252");
1

For me CP1252 didn't work because it failed with non alphanumeric symbols. The best charset I found is ISO-8859-1, use as follows:

Base64.getEncoder()
                .encodeToString(
                    stringToBeEncoded.getBytes(
                        Charset.forName("ISO-8859-1")))

Comments

0

I used this one:

private String decodeBase64(String encrypted) {
    byte[] encoded = new byte[0];
    try {
        encoded = Base64.getDecoder().decode(encrypted.getBytes("CP1252"));
        String str = new String(encoded, "CP1252");
        return str;
    } catch (UnsupportedEncodingException e) {

        e.printStackTrace();
    }
    return "";
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.