Encoding/decoding of data between PHP/Java for Android

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(‡=ñö

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")))