0.1, the character encoding and encryption -Base64 several implementations in Java

Foreword

Performance status prior to the state of mind, habits prior to determination, focus first on preferences

Special statement

This paper lists several implementations of the Java environment Base64 There are several things in particular:
1, part of the method will need to download a separate jar package, provided herein Maven dependent address, to download
2, JDK1.8 provided so far the most efficient implementation, Spring of the package is provided separately JDK1.8 method, so attention JDK version
3, a test paper include a plurality of class test methods simultaneously, so that the conventional import declaration class can not be used (because different Base64 implementation of the same class name), so I wrote a particular path for each tool type in the code, the actual work to restore the head of the class reference import

Base64 encoding and decoding is not encryption algorithm

Base64 only provides a way of bytes into characters, this transformation does not involve encryption and decryption
characters do not belong to any conversion Base64 encoding format, a string is read, it can be different as transfer form between the coding format - such as a network transmission - though you can also use byte transmitted directly

Closed, the Code

Methods Apache Commons Codec provide advice to use jdk1.8, or if.
And sun.mis c kit and Base64 codec Apache Commons Codec provided to compare, then,
Base64 provided by the Java 8 has a better performance. The actual test speed encoding and decoding words,
Base64 provided by the Java 8, than sun.mis c suite provides even faster at least 11 times
faster at least three times faster than Apache Commons Codec provides.

package com.bestcxx.stu.test;

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

import org.junit.Test;
/**
 * <h3>
 * base64 字符串(字符类型)-二进制字节数组-base64字符串-二进制字节数组-字符串(字符类型)
 * </h3>
 * @author jie.wu
 */
public class Base64Test {
	
	/**
	 * jdk sun.misc套件
	 * import sun.misc.BASE64Encoder;
	 * import sun.misc.BASE64Decoder;
	 */
	@Test
	public void test1() throws UnsupportedEncodingException,IOException{
		//编码对象
		sun.misc.BASE64Encoder encoder=new sun.misc.BASE64Encoder();
		//解码对象
		sun.misc.BASE64Decoder decoder=new sun.misc.BASE64Decoder();
		
		//字符串
		String temStr="你好";
		//字符串>字节数组
		byte[] bytesA=temStr.getBytes("UTF8");
		//编码:字节数组>base64字符串
		String resultA=encoder.encode(bytesA);
		//解码:base64字符串>字节数组
		byte[] bytesB=decoder.decodeBuffer(resultA);
		//字节数组>字符串
		String resultB=new String(bytesB,"UTF8");
		System.out.println("resultB="+resultB);
		
	}
	
	/**
	 * bouncy castle
	 * https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on
	 * import org.bouncycastle.util.encoders.Base64
	 * @throws UnsupportedEncodingException 
	 */
	@Test
	public void test2() throws UnsupportedEncodingException {
		org.bouncycastle.util.encoders.Base64 base64=new org.bouncycastle.util.encoders.Base64();
		//字符串
		String temStr="你好";
		//字符串>字节数组
		byte[] bytesA=temStr.getBytes("UTF8");
		//编码:字节数组>base64字符串
		String resultA=base64.toBase64String(bytesA);
		//解码:base64字符串>字节数组
		byte[] bytesB=base64.decode(resultA);
		//字节数组>字符串
		String resultB=new String(bytesB,"UTF8");
		System.out.println("resultB="+resultB);
	}
	
	/**
	 * commons-codec
	 * https://mvnrepository.com/artifact/commons-codec/commons-codec
	 * import org.apache.commons.codec.binary.Base64;
	 * @throws UnsupportedEncodingException 
	 */
	@Test
	public void test3() throws UnsupportedEncodingException {
		org.apache.commons.codec.binary.Base64 base64 = new org.apache.commons.codec.binary.Base64();
		//字符串
		String temStr="你好";
		//字符串>字节数组
		byte[] bytesA=temStr.getBytes("UTF8");
		//编码:字节数组>base64字符串
		String resultA=base64.encodeToString(bytesA);
		//解码:base64字符串>字节数组
		byte[] bytesB=base64.decode(resultA);
		//字节数组>字符串
		String resultB=new String(bytesB,"UTF8");
		System.out.println("resultB="+resultB);
		
	}
	
	/**
	 * jdk 1.8
	 * import java.util.Base64
	 * @throws UnsupportedEncodingException 
	 */
	@Test
	public void test4() throws UnsupportedEncodingException {
		//编码对象
		java.util.Base64.Encoder encoder=java.util.Base64.getEncoder();
		//解码对象
		java.util.Base64.Decoder decoder=java.util.Base64.getDecoder();
		//字符串
		String temStr="你好";
		//字符串>字节数组
		byte[] bytesA=temStr.getBytes("UTF8");
		//编码:字节数组>base64字符串
		String resultA=encoder.encodeToString(bytesA);
		//解码:base64字符串>字节数组
		byte[] bytesB=decoder.decode(resultA);
		//字节数组>字符串
		String resultB=new String(bytesB,"UTF8");
		System.out.println("resultB="+resultB);
		
	}
	/**
	 * Spring core
	 * https://mvnrepository.com/artifact/org.springframework/spring-core
	 * import org.springframework.util.Base64Utils
	 * 看源码可以看到内部也提供了对 jdk1.8 提供的方法的封装-据说 jdk1.8 是效率最高的
	 * @throws UnsupportedEncodingException 
	 */
	@Test
	public void test5() throws UnsupportedEncodingException {
		//字符串
		String temStr="你好";
		//字符串>字节数组
		byte[] bytesA=temStr.getBytes("UTF8");
		//编码:字节数组>base64字符串
		String resultA=org.springframework.util.Base64Utils.encodeToString(bytesA);
		//解码:base64字符串>字节数组
		byte[] bytesB=org.springframework.util.Base64Utils.decodeFromString(resultA);
		//字节数组>字符串
		String resultB=new String(bytesB,"UTF8");
		System.out.println("resultB="+resultB);
		
	}

}

Reference links

[1] https://www.cnblogs.com/alter888/p/9140732.html
[2] https://blog.csdn.net/u012187452/article/details/83239117
[3] https: // www.zhihu.com/question/38036594

Guess you like

Origin blog.csdn.net/bestcxx/article/details/92076954