BASE64 Encode Decode

package com.humi.encryption;

import java.io.IOException;
import java.io.UnsupportedEncodingException;


import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * @since 2017年8月3日10:14:00
 * @author Penny
 * @category 加解密 BASE64Encoder BASE64Decoder;
 */
public class EncryptionTest {

	public static void main(String[] args) {
		
		String words1 = "this is a test words by 2017年8月3日10:16:24";
		String words2 = "你看的全是乱码!QQAQ!";
//		StringBuilder sb;
		BASE64Encoder enc = new BASE64Encoder();
		BASE64Decoder dec = new BASE64Decoder();
		
		try {
			 words1 = enc.encode(words1.getBytes("utf-8"));
			 words2 = enc.encode(words2.getBytes("utf-8"));
			 System.out.println("===========编码后===========");
			 System.out.println(words1);
			 System.out.println(words2);
			
			 
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		 
			 try {
				words1 = new String(dec.decodeBuffer(words1),"utf-8");
				words2 = new String(dec.decodeBuffer(words2),"utf-8");
				System.out.println("===========解码后===========");
				System.out.println(words1);
				System.out.println(words2);
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
}

Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,在发送电子邮件时,服务器认证的用户名和密码需要用Base64编码,附件也需要用Base64编码。
Base64要求把每三个8Bit的字节转换为四个6Bit的字节(3*8 = 4*6 = 24),然后把6Bit再添两位高位0,组成四个8Bit的字节,也就是说,转换后的字符串理论上将要比原来的长1/3。 
原文的字节最后不够3个的地方用0来补足,转换时Base64编码用=号来代替。这就是为什么有些Base64编码会以一个或两个等号结束的原因,但等号最多只有两个。
``
原文地址:https://www.cnblogs.com/humi/p/7278388.html