BASE64 encoding algorithm of Android data encryption

Source: Internet
Author: User
Tags md5 encryption printable characters


In front of the study summed up the usual development encountered in various data encryption methods, the end will be encrypted binary data Base64 encoding, play a two times the effect of encryption, in fact, Base64 from the strict sense is not a cryptographic algorithm, but a coding algorithm, Why use BASE64 encoding? What is it that solves the problem? That's what this article is about? Here are additional data encryption link addresses:

    • RSA encryption of Android data encryption

    • AES encryption of Android data encryption

    • Des encryption of Android Data encryption

    • MD5 encryption of Android data encryption

What Base64 algorithm?

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

    • Standard BASE64 only 64 characters (English case, number and +,/) and used as suffix equals sign;
    • Base64 is to turn 3 bytes into 4 printable characters, so the BASE64 encoded string must be divisible by 4 (not counted as an equal sign for the suffix);
    • The equals sign must be used as a suffix, and the number must be 0, one, or 2. This is because if the original length cannot be divisible by 3, the Base64 is added in the rear to gather the 3n bits. In order to restore correctly, add a few plus equals. It is obvious that the number of equals can only be 0, 1, or 2;
    • Strictly speaking, Base64 can not be considered as a kind of encryption, but it is encoded conversion.

Encode table for Base64

What is the use of BASE64 encoding?

Any data in the computer is stored in ASCII code, and the value between the 128~255 of the ASCII code is invisible. When exchanging data on the network, for example, from A to B, often through a number of routing devices, because different devices on the processing of characters have some differences, so that those invisible characters can be processed errors, which is not conducive to transmission. So first make the data a Base64 code, all become visible characters, so the likelihood of error is greatly reduced.

Base64 specifically implement 

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 the file
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()];;
    encodedString = Base64.encodeToString(buffer, Base64.DEFAULT);
    Log.e("Base64", "Base64---->" + encodedString);
} catch (Exception e) {
4.) Base64 Decoding of files
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);
} catch (Exception e) {
5.) for Base64.default parameter description

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

    • Default this parameter is defaulted, using the default method to encrypt

    • No_padding This parameter is omitted the last "=" of the cryptographic string

    • No_wrap This parameter means omitting all line breaks (CRLF is useless after setting)

    • CRLF This parameter looks familiar, it is a win-style line break, meaning to use CR LF this pair as the end of a line instead of Unix-style LF

    • Url_safe This parameter means that encryption does not use characters that have special meaning to the URL and file name as an encrypted character, which is replaced by-and _ + and/


Base64 coding seems simple, but it is widely used in practical development. The current project is only used so much, and later in the use of more complex situations to supplement.

BASE64 encoding algorithm of Android data encryption

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.