Simple implementation of BASE64 encoding algorithm for Android data encryption _android

Source: Internet
Author: User
Tags base64 printable characters

The previous study summed up the usual development of the various data encryption methods, the final will be encrypted binary data for BASE64 encoding, played a two times encryption effect, in fact, Base64 from the strict sense is not a cryptographic algorithm, but a coding algorithm, Why use BASE64 encoding? What's the problem it solves? Is that what this article is about?

What Base64 algorithm?

Base64 is one of the most common encoding methods for transmitting 8Bit byte code in the network, BASE64 is not a security domain encryption algorithm, in fact, Base64 can only be regarded as an encoding algorithm, the data content encoding to fit the transmission. Standard BASE64 code decoding is completely reversible without additional information, even if you own custom character set design a class BASE64 encoding is used for data encryption and is easier to crack in most scenarios. BASE64 encoding is essentially a scheme for converting binary data into text data. For non binary data, it is first converted into binary form, and then every 6 bits (2 of 6 =64) to compute its decimal value, according to the value of the 64 characters in the a--z,a--z,0--9,+,/ to find the corresponding characters, and finally get a text string. The basic rules are as follows:

The standard Base64 is only 64 characters (English case, number and +,/) and is used as the suffix equal; Base64 is to turn 3 bytes into 4 printable characters, so the BASE64 encoded string must be divisible by 4 (not counted as the equal sign of the suffix); the equal sign must be used as a suffix, And the number must be 0, one or 2. This is because if the length of the original text is not divisible by 3, BASE64 will add the following to the 3n bit. In order to restore correctly, add a few plus equals. Obviously the number of equals can only be 0, 1, or 2; Strictly speaking, Base64 is not a kind of encryption, it can only be said that the code conversion.

The following figure is the BASE64 encoding table

What is the use of Base64 coding?

Any data in the computer is stored in ASCII code, and the value between the 128~255 of the ASCII code is not visible. In the exchange of data on the network, for example, from A to B, often through a number of routing devices, because the different devices to handle the character of a number of different ways, so that those invisible characters can be handled errors, which is not conducive to transmission. So first the data to do a Base64 code, all become visible characters, so the likelihood of error is greatly reduced.

Base64 Concrete Implementation

1.) String for BASE64 encoding

String encodedstring = base64.encodetostring ("WHOISLCJ". GetBytes (), base64.default);
 LOG.E ("Base64", "Base64---->" + encodedstring);

2.) String for Base64 decoding

 String decodedstring =new string (Base64.decode (Encodedstring,base64.default));
 LOG.E ("Base64", "Base64---->" + decodedstring);

3.) Base64 Encoding of files

File File = new file ("/storage/emulated/0/pimsecure_debug.txt");
FileInputStream inputfile = null;
try {
  inputfile = new FileInputStream (file);
  byte[] buffer = new byte[(int) file.length ()]; (buffer);
  Inputfile.close ();
  encodedstring = base64.encodetostring (buffer, base64.default);
  LOG.E ("Base64", "Base64---->" + encodedstring);
} catch (Exception e) {
  e.printstacktrace ();

4.) Base64 decoding of the file

File Desfile = new file ("/storage/emulated/0/pimsecure_debug_1.txt");
FileOutputStream fos = null;
try {
  byte[] decodebytes = Base64.decode (Encodedstring.getbytes (), base64.default);
  FOS = new FileOutputStream (desfile);
  Fos.write (decodebytes);
  Fos.close ();
} catch (Exception e) {
  e.printstacktrace ();

5.) for Base64.default parameter description

Both encoding and decoding have a parameter flags,android provides the following

Default for this parameter is defaults, using the default method to encrypt

No_padding This parameter is to omit the last "=" of the encrypted string.

No_wrap This parameter means to omit all line breaks (CRLF is useless after setting)

CRLF This parameter looks familiar, it is a win-style line break, which means using CR LF as the end of a line rather than the Unix-style LF

Url_safe This parameter means that when encrypting, you do not use characters that have special meaning for URLs and filenames as cryptographic characters, specifically to-and _ instead of + and/


Base64 coding seems simple, but it is used quite extensively in actual development. At present, the project is only used so much, in the future to use more complex situation to supplement.

The above is a small series for everyone to bring the Android data encryption Base64 coding algorithm for the simple realization of all content, I hope that we support cloud-Habitat Community ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.