For encoding and decoding we have Base64 class in android.
For encoding in String we can use encodeToString (byte[] input, int flags)
This method Base64-encode the given data and return a newly allocated String with the result.
Parameters
input-> the data to encode
flags-> controls certain features of the encoded output. Passing DEFAULT results in output that adheres to RFC 2045.
Example:-
public static String getB64Auth(String key) {
String ret = Base64.encodeToString(key.getBytes(), Base64.DEFAULT);
return ret;
}
For decoding encoded String we can use decode (String str, int flags)
This method Decode the Base64-encoded data in input and return the data in a new byte array.
Parameters
str-> the input String to decode, which is converted to bytes using the default charset
flags-> controls certain features of the decoded output. Pass DEFAULT to decode standard Base64.
Example:-
public static String getDecodedString(String key) {
byte[] bytes = Base64.decode(key, Base64.DEFAULT);
return new String(bytes);
}
0 Comment(s)