android -------- Base64 encryption and decryption algorithms

Base64 is one of the most common network for the transmission of 8Bit encoding bytecode, Base64 is based on 64 printable characters to binary data representation. To see RFC2045 ~ RFC2049, MIME above detailed specification.


Base64 encoded character from binary to process, it can be used to deliver a longer identification information HTTP environment. For example, in the Java Persistence Hibernate system, on the use of a longer Base64 to the unique identifier (typically a 128-bit UUID) encoded as a string, and forms as an HTTP HTTP GET URL parameters. In other applications, often you need to encode binary data to fit on the URL (including hidden form fields) forms. In this case, Base64 encoded has not readable, need to read after decoding.

 

Before Java8.0, add Jar package


After the Java approach. 8
Java. 8 in java.util suite, a new category of Base64, may be used Base64 encoding and decoding process, is used as follows:

import java.nio.charset.StandardCharsets;

public class ABase64 {

    public static void main(String[] args) {
        String password = "Hello 123456";
        //加密
        String encoded = java.util.Base64.getEncoder().encodeToString(password.getBytes(StandardCharsets.UTF_8));
        //解密
        String decoded = new String(java.util.Base64.getDecoder().decode(encoded), StandardCharsets.UTF_8);
        System.out.println(encoded);
        System.out.println(decoded);
        showBase64();
    }


    private static void showBase64() {

        try {
            final java.util.Base64.Decoder decoder = java.util.Base64.getDecoder();
            final java.util.Base64.Encoder encoder = java.util.Base64.getEncoder();
            final String text = "Hello 小笨蛋";
            final byte[] textByte = text.getBytes("UTF-8");
            //编码
            final String encodedText = encoder.encodeToString(textByte);
            System.out.println(encodedText);
            //解码
            System.out.println(new String(decoder.decode(encodedText), "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 

Log:

 

Guess you like

Origin www.cnblogs.com/zhangqie/p/10979739.html