Reputation: 292
I want to understand why there is a difference between encoding in openssl commandline to those done via android.util.Base64
// contents of data.out = "a"
openssl base64 -A -in data.out -out data_enc.out
cat data_enc.out
YQo=
I tried using android's Base64, but couldn't get the same output. I tried with diff charsets and all possible Flag values.
String str = "a";
int[] flags = new int[]{Base64.DEFAULT, Base64.CRLF, Base64.NO_CLOSE, Base64.NO_PADDING,
Base64.NO_WRAP, Base64.URL_SAFE};
int count= 0;
for(int flag: flags) {
String encStr = Base64.encodeToString(str.getBytes(StandardCharsets.UTF_8), flag);
android.util.Log.i("CERT_LOG", "UTF_8: " + (count) + " Encoded data value in code " + encStr);
encStr = Base64.encodeToString(str.getBytes(StandardCharsets.UTF_16LE), flag);
android.util.Log.i("CERT_LOG",
"UTF_16LE: "+ (count) + " Encoded data value in code " + encStr);
encStr = Base64.encodeToString(str.getBytes(StandardCharsets.UTF_16), flag);
android.util.Log.i("CERT_LOG",
"UTF_16: "+ (count) +" Encoded data value in code " + encStr);
encStr = Base64.encodeToString(str.getBytes(StandardCharsets.UTF_16BE), flag);
android.util.Log.i("CERT_LOG",
"UTF_16BE: "+ (count) +" Encoded data value in code " + encStr);
encStr = Base64.encodeToString(str.getBytes(StandardCharsets.US_ASCII), flag);
android.util.Log.i("CERT_LOG",
"US_ASCII: "+ (count) +" Encoded data value in code " + encStr);
encStr = Base64.encodeToString(str.getBytes(StandardCharsets.ISO_8859_1), flag);
android.util.Log.i("CERT_LOG", "ISO_8859_1: "+ (count++) +" Encoded data value in code " + encStr);
}
But none of the output matched with those from commandline. Can someone throw some light on what am I missing.
Upvotes: 0
Views: 320
Reputation: 111239
Your text file includes a line feed at the end of the file. To get the same output on Android, add \n to your input string:
String str = "a\n";
Upvotes: 1