Base64加密
Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一。
- Base64编码具有不可读性
- 简单加密
- 可以做HTTP GET URL中的参数
Base64工具类提供了一套静态方法获取下面三种编解码器:
- 基本:输出被映射到一组字符A-Za-z0-9+/,编码不添加任何行标,输出的解码仅支持A-Za-z0-9+/。
- URL:输出映射到一组字符A-Za-z0-9+_,输出是URL和文件。
- MIME:输出隐射到MIME友好格式。输出每行不超过76字符,并且使用’\r’并跟随’\n’作为分割。编码输出最后没有行分割。
Base64
内嵌类:
内嵌类 | 描述 |
---|---|
static class Base64.Decoder | 该类实现一个解码器用于,使用 Base64 编码来解码字节数据。 |
static class Base64.Encoder | 该类实现一个编码器,使用 Base64 编码来编码字节数据。 |
方法:
方法名 | 描述 |
---|---|
static Base64.Decoder getDecoder() | 返回一个 Base64.Decoder ,解码使用基本型 base64 编码方案。 |
static Base64.Encoder getEncoder() | 返回一个 Base64.Encoder ,编码使用基本型 base64 编码方案。 |
static Base64.Decoder getMimeDecoder() | 返回一个 Base64.Decoder ,解码使用 MIME 型 base64 编码方案。 |
static Base64.Encoder getMimeEncoder() | 返回一个 Base64.Encoder ,编码使用 MIME 型 base64 编码方案。 |
static Base64.Encoder getMimeEncoder(int lineLength, byte[] lineSeparator) | 返回一个 Base64.Encoder ,编码使用 MIME 型 base64 编码方案,可以通过参数指定每行的长度及行的分隔符。 |
static Base64.Decoder getUrlDecoder() | 返回一个 Base64.Decoder ,解码使用 URL 和文件名安全型 base64 编码方案。 |
static Base64.Encoder getUrlEncoder() | 返回一个 Base64.Encoder ,编码使用 URL 和文件名安全型 base64 编码方案。 |
代码块 例如:
package com.jasonk.datatypeconversion;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;
public class Demo_002_Base64 {
/**
* 数据加密
* @param value 加密数据
* @return
* @throws Exception
*/
public static String enCoderByBASE64(String value) throws Exception{
String enCoderValue = null;
if(null!=value && value.trim().length()>0){
try {
byte[] vB = value.getBytes("utf-8");
enCoderValue = new BASE64Encoder().encode(vB);
} catch (UnsupportedEncodingException e) {
throw e;
}
}
return enCoderValue;
}
public static String enCoderByBASE64E(String value) throws Exception{
String enCoderValue = null;
if(null!=value && value.trim().length()>0){
try {
byte[] vB = value.getBytes("utf-8");
enCoderValue = Base64.getEncoder().encodeToString(vB);
} catch (UnsupportedEncodingException e) {
throw e;
}
}
return enCoderValue;
}
public static String enCoderByBASE64EUrl(String value) throws Exception{
String enCoderValue = null;
if(null!=value && value.trim().length()>0){
try {
byte[] vB = value.getBytes("utf-8");
enCoderValue = Base64.getUrlEncoder().encodeToString(vB);
} catch (UnsupportedEncodingException e) {
throw e;
}
}
return enCoderValue;
}
/**
* 数据解密
* @param enCoderValue 解密数据
* @return
* @throws Exception
*/
public static String deCoderByBASE64(String enCoderValue) throws Exception{
String value = null;
if(null!=enCoderValue && enCoderValue.trim().length()>0){
try {
byte[] vB = new BASE64Decoder().decodeBuffer(enCoderValue);
value = new String(vB, "utf-8");
} catch (IOException e) {
throw e;
}
}
return value;
}
public static String deCoderByBASE64E(String enCoderValue) throws Exception{
String value = null;
if(null!=enCoderValue && enCoderValue.trim().length()>0){
try {
byte[] vB = enCoderValue.getBytes();
value = new String(Base64.getDecoder().decode(vB), "utf-8");
} catch (IOException e) {
throw e;
}
}
return value;
}
public static String deCoderByBASE64EUrl(String enCoderValue) throws Exception{
String value = null;
if(null!=enCoderValue && enCoderValue.trim().length()>0){
try {
byte[] vB = enCoderValue.getBytes();
value = new String(Base64.getUrlDecoder().decode(vB), "utf-8");
} catch (IOException e) {
throw e;
}
}
return value;
}
public static void main(String[] args) {
try {
String enCoderV = Demo_002_Base64.enCoderByBASE64("这么low的加密,竟然有人用!");
System.out.println("加密数据-包装类 :"+enCoderV);
String value = Demo_002_Base64.deCoderByBASE64(enCoderV);
System.out.println("解密数据-包装类 :"+value);
enCoderV = Demo_002_Base64.enCoderByBASE64E("这么low的加密,竟然有人用!");
System.out.println("加密数据-基本类 :"+enCoderV);
value = Demo_002_Base64.deCoderByBASE64E(enCoderV);
System.out.println("解密数据-基本类 :"+value);
enCoderV = Demo_002_Base64.enCoderByBASE64EUrl("这么low的加密,竟然有人用!");
System.out.println("加密数据-基本Url:"+enCoderV);
value = Demo_002_Base64.deCoderByBASE64EUrl(enCoderV);
System.out.println("解密数据-基本Url:"+value);
} catch (Exception e) {
e.printStackTrace();
}
}
}