1

I am trying to transform a string to byte and then get again as string, but the result was different. Have a look:

To decode:

byte[] tokenInBytes = Base64.decode(token, mFlags);
mTokenPreference.put(Base64.encodeToString(tokenInBytes, mFlags));

To get like a string again:

String value = Base64.encodeToString(tokenInBytes, mFlags);

The original string (before decode): eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVhMDA2ZTI4OGQ4ZDc1Z

And after was (after encode): eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9eyJpZCI6IjVhMDA2ZTI4OGQ4ZDc1Z

Dots was removed. Anyone knows what I did wrong?

The value of mFlags was:

int mFlags = Base64.NO_WRAP | Base64.URL_SAFE | Base64.NO_PADDING;
Rafaela Lourenço
  • 1,126
  • 16
  • 33

2 Answers2

0

You can replace your string value (dot) with any letter then after encode and decode replace same letter with dot.

ankur bhut
  • 125
  • 1
  • 11
0

I got this code from this link

// Sending side
byte[] data = value.getBytes("UTF-8");
String base64 = Base64.encodeToString(data, Base64.DEFAULT);

// Receiving side
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String value = new String(data, "UTF-8");
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Vivek Barai
  • 1,338
  • 13
  • 26
  • @RafaelaLourenço as it a special character i think you should do like this String encodeStr =URLEncoder.encode( yourstring, "UTF-8" ); // Sending side byte[] data = value.getBytes("UTF-8"); String base64 = Base64.encodeToString(encodeStr, Base64.DEFAULT); // Receiving side byte[] data = Base64.decode(base64, Base64.DEFAULT); String value = new String(data, "UTF-8"); String decodeString = URLDecoder.decode( value, "UTF-8" ); – Vivek Barai Dec 05 '17 at 17:04