0.1、字符编码和加密-Base64 在 Java 中的几个实现方式

前言

体能状态先于精神状态,习惯先于决心,聚焦先于喜好

特别声明

本文 列举了 Java 环境下Base64的几种实现方式 ,有几点需要特别声明:
1、部分方法需要下载单独的 jar 包,本文提供了 Maven 依赖地址,可自行下载
2、JDK1.8 提供了目前为止最高效的实现方式,Spring 也单独提供了对JDK1.8方法的封装,所以注意JDK版本
3、本文在一个测试类同时列举多个测试方法,所以常规 import 类的声明无法使用了(因为不同的实现方式的Base64类名字相同),所以我在代码里写了每个工具类的具体路径,实际工作中请恢复成类的头部 import 的引用方式

Base64 的编码和解码不是加密算法

Base64 只是提供了一种对字节转化为字符的一种方式,这种转化不涉及加密和解密
Base64 转化的字符不属于任何一种编码格式,是看不懂的字符串,所以它可以作为不同编码格式之间的中转形式——比如用于网络传输——尽管你也可以直接用字节进行传输

关门,上代码

建议使用 jdk1.8 提供的方法,或者 Apache Commons Codec 提供的方法.
与sun.mis c套件和Apache Commons Codec所提供的Base64编解码器来比较的话,
Java 8提供的Base64拥有更好的效能。实际测试编码与解码速度的话,
Java 8提供的Base64,要比sun.mis c套件提供的还要快至少11倍,
比Apache Commons Codec提供的还要快至少3倍。

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);
		
	}

}

参考链接

[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

猜你喜欢

转载自blog.csdn.net/bestcxx/article/details/92076954
今日推荐