Для кодирования:
private String encodeString(String s) {
byte[] data = new byte[0];
try {
data = s.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
String base64Encoded = Base64.encodeToString(data, Base64.DEFAULT);
return base64Encoded;
}
}
Для декодирования:
private String decodeString(String encoded) {
byte[] dataDec = Base64.decode(encoded, Base64.DEFAULT);
String decodedString = "";
try {
decodedString = new String(dataDec, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
return decodedString;
}
}
Пример
String text = "example007";
Log.e("encoded", encodeString(text)); //Output: ZXhhbXBsZTAwNw==
Log.e("decoded", decodeString(encodeString(text))); //Output: example007