Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Android how to concatenate base64 encoded strings?

Tags:

android

Please help me to solve this:

I have two strings for email-id and password like

String name = "[email protected]";
String pass = "abc";

I encode these two into Base64 string like

String encoded_name = new String(Base64.encode(name.getBytes(), 0));
String encoded_pass = new String(Base64.encode(pass.getBytes(), 0));

and I need to concatenate these two encoded strings with space like

String merge = encoded_name + " " + encoded_pass;

I checked this string in console by

System.out.print("Concatenate string= " + merge);

but in console I am getting result in two lines like this

11-18 00:25:29.898: INFO/System.out(1244): Merge= eHl6QGdtYWlsLmNvbQ==
11-18 00:25:29.908: INFO/System.out(1244):  YWJj

Why is this happing the result is unexpected for me why it is not printing in a single line. please help me to solve this.

Thanks

like image 397
Ujjwal Bansal Avatar asked Nov 17 '11 19:11

Ujjwal Bansal


People also ask

What is CG == in Base64?

Cg== is the base64 encode of the new line character in the latest position. So if you want to encode ABC you will get QUJD , however if you include a "return character" after ABC you will get QUJDCg== .

Can Base64 be cracked?

Your provider has to tell you the encryption algorithm and key. From the length of the base64 representation, it could be pretty much anything. Yet, as the other answer correctly said, you have no chance anyone can crack this for you here; without a key and the encryption method you will not get hold of the data.

Can Base64 encoded string contain?

Base64 is a binary-to-text encoding scheme. It's represented as printable ASCII characters where each Base64 character contains 6 bits of binary information. It's very useful for storing image/audio information in Strings of information.


2 Answers

You should use the NO_WRAP flag as described in the Docs, the Base64 class will not add additional newlines.

NO_WRAP: Encoder flag bit to omit all line terminators (i.e., the output will be on one long line).

So change your lines to the following:

String encoded_name = new String(Base64.encode(name.getBytes(), Base64.NO_WRAP));
String encoded_pass = new String(Base64.encode(pass.getBytes(), Base64.NO_WRAP));

This will output the following:

11-17 19:16:51.283: INFO/System.out(354): Concatenate string= eHl6QGdtYWlsLmNvbQ== YWJj
like image 87
Piro Avatar answered Oct 17 '22 04:10

Piro


Please have a look on Android Api reference Document. It will solve all other queries regarding Base64 encoding/decoding in android.

one more efficient way to solve your problem:

String encoded_name = Base64.encodeToString(name.getBytes("utf-8"), Base64.NO_WRAP);
String encoded_pass = Base64.encodeToString(pass.getBytes("utf-8"), Base64.NO_WRAP);

out put:-

11-17 10:55:27.492: V/BASE-64-.encodeToString(525): eHl6QGdtYWlsLmNvbQ== YWJj
like image 1
Rupesh Yadav Avatar answered Oct 17 '22 03:10

Rupesh Yadav