Sometimes in your application you contain some data which is very useful to you or its so sensitive that you don't want anyone else to have it. In that you need to encode or encrypt your data while transferring it through your application.
So for encoding your data and use it in your android application, we have android.util.Base64 class which is used to encode/decode your data.
First of all encoding is not the same thing as encryption. If you’re storing or using useful and sensitive information, you should be encrypting, not simply Base64 encoding. But android.util.Base64 class is also a very useful tool for this purpose.
The use of this class is very simple as depicted in the below example:
1. First import the required class:
import android.util.Base64;
2. Now just use the below code:
String str = "Hello, world!";
byte[] encodedStr = Base64.encode(str.getBytes(), Base64.DEFAULT);
byte[] decodedStr = Base64.decode(encodedStr, Base64.DEFAULT);
Log.d("Android Castle", "Android Castle Default Value = " + str);
Log.d("Android Castle", "Android Castle Encoded Value = " + new String(encodedStr));
Log.d("Android Castle", "Android Castle Decoded Value = " + new String(decodedStr));
3. The following will be out put of the above snippet:
Android Castle Default Value = Hello, world!
Android Castle Encoded Value = SGVsbG8sIHdvcmxkIQ==
Android Castle Decoded Value = Hello, world!
But in many case we have some highly secure data where the data is encoded and then compressed and then its being transferred to your application. In that case being a developer you have to decode the data and decompress it as well to make it useful for your application. The solution for this problem is as follows:
1. Import as follows:
import org.java_websocket.util.Base64;
2. Use the following code snippet:
String st = "YOUR ENCODE STRING";
byte[] decodedByte = new byte[0];
byte[] result = new byte[0];
int resultLength = 0;
try {
decodedByte = Base64.decode(st, Base64.GZIP);
Inflater decompresser = new Inflater();
decompresser.setInput(decodedByte, 0, decodedByte.length);
result = new byte[decodedByte.length];
resultLength = decompresser.inflate(result);
decompresser.end();
String outputString = new String(result, 0, resultLength, "UTF-8");
System.out.println("Your decode string: " + outputString);
} catch (Exception e) {
e.printStackTrace();
}
That's all.
Share and comment if any issues.
So for encoding your data and use it in your android application, we have android.util.Base64 class which is used to encode/decode your data.
First of all encoding is not the same thing as encryption. If you’re storing or using useful and sensitive information, you should be encrypting, not simply Base64 encoding. But android.util.Base64 class is also a very useful tool for this purpose.
The use of this class is very simple as depicted in the below example:
1. First import the required class:
import android.util.Base64;
2. Now just use the below code:
String str = "Hello, world!";
byte[] encodedStr = Base64.encode(str.getBytes(), Base64.DEFAULT);
byte[] decodedStr = Base64.decode(encodedStr, Base64.DEFAULT);
Log.d("Android Castle", "Android Castle Default Value = " + str);
Log.d("Android Castle", "Android Castle Encoded Value = " + new String(encodedStr));
Log.d("Android Castle", "Android Castle Decoded Value = " + new String(decodedStr));
3. The following will be out put of the above snippet:
Android Castle Default Value = Hello, world!
Android Castle Encoded Value = SGVsbG8sIHdvcmxkIQ==
Android Castle Decoded Value = Hello, world!
But in many case we have some highly secure data where the data is encoded and then compressed and then its being transferred to your application. In that case being a developer you have to decode the data and decompress it as well to make it useful for your application. The solution for this problem is as follows:
1. Import as follows:
import org.java_websocket.util.Base64;
2. Use the following code snippet:
String st = "YOUR ENCODE STRING";
byte[] decodedByte = new byte[0];
byte[] result = new byte[0];
int resultLength = 0;
try {
decodedByte = Base64.decode(st, Base64.GZIP);
Inflater decompresser = new Inflater();
decompresser.setInput(decodedByte, 0, decodedByte.length);
result = new byte[decodedByte.length];
resultLength = decompresser.inflate(result);
decompresser.end();
String outputString = new String(result, 0, resultLength, "UTF-8");
System.out.println("Your decode string: " + outputString);
} catch (Exception e) {
e.printStackTrace();
}
That's all.
Share and comment if any issues.
No comments:
Post a Comment