按照RFC2045的定义,Base64被定义为:Base64内容传送编码被设计用来把任意序列的8位字节描述为一种不易被人直接识别的形式。
BASE64 编码是一种常用的字符编码,在很多地方都会用到。但base64不是安全领域下的加密解密算法。能起到安全作用的效果很差,而且很容易破解,他核心作用应该是传输数据的正确性,有些网关或系统只能使用ASCII字符。Base64就是用来将非ASCII字符的数据转换成ASCII字符的一种方法,而且base64特别适合在http,mime协议下快速传输数据。
在Java 8在java.util包下面实现了BASE64编解码API,而且性能不俗,API也简单易懂,下面展示下这个类的使用例子。
原文:世界和平
BASE64加密后:ysC957rNxr0=
jdk8中的Base64针对性的给出了三组编解码接口针对与三种特定的应用场景:基本应用、url、mime,三组接口中的方法很像,只是在内部实现时针对特定场景进行了相应的差异化处理。
关于jdk8的Base64介绍的文章感觉DZone的一篇简要清晰,链接如下:
下面给出junit test case
public class Base64Test {
Logger logger = Logger.getLogger(this.getClass().getName());
String charset_utf8 = "UTF-8";
private String srcStr = "壹贰叁肆伍陆柒捌玖拾佰仟万亿零整元角分";
private String srcUrl = "https://www.baidu.com/s?wd=jdk&rsv_page=1¶=中国";
@Test
public void basicTest() {
logger.info("basec test");
try {
byte[] enStrBytes = Base64.getEncoder().encode(srcStr.getBytes(Charset.forName(charset_utf8)));
String enStr = Base64.getEncoder().encodeToString(srcStr.getBytes(charset_utf8));
System.out.println(new String(enStrBytes, charset_utf8));
System.out.println(enStr);
byte[] deStrBytes = Base64.getDecoder().decode(enStrBytes);
byte[] deStrBytes1 = Base64.getDecoder().decode(enStr);
logger.info(new String(deStrBytes, charset_utf8));
logger.info(new String(deStrBytes1, charset_utf8));
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Base64Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Test
public void urlTest() {
logger.info("url test");
try {
byte[] enStrBytes = Base64.getUrlEncoder().encode(srcUrl.getBytes(charset_utf8));
String enStr = Base64.getUrlEncoder().encodeToString(srcUrl.getBytes(charset_utf8));
System.out.println(new String(enStrBytes, charset_utf8));
System.out.println(enStr);
byte[] deStrBytes = Base64.getUrlDecoder().decode(enStrBytes);
byte[] deStrBytes1 = Base64.getUrlDecoder().decode(enStr);
logger.info(new String(deStrBytes, charset_utf8));
logger.info(new String(deStrBytes1, charset_utf8));
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Base64Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Test
public void mimeTest() {
logger.info("mime test");
try {
byte[] toEncode = srcStr.getBytes(charset_utf8);
String mimeEncoded = Base64.getMimeEncoder().encodeToString(toEncode);
System.out.println(mimeEncoded);
String deMimeStr = new String(Base64.getMimeDecoder().decode(mimeEncoded), charset_utf8);
logger.info(deMimeStr);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Base64Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Test
public void wrapTest() {
logger.info("wrap test");
String src = "This is the content of any resource read from somewhere"
+ " into a stream. This can be text, image, video or any other stream.";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (OutputStream os = Base64.getEncoder().wrap(baos)) {
os.write(src.getBytes("utf-8"));
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Base64Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Base64Test.class.getName()).log(Level.SEVERE, null, ex);
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
try (InputStream is = Base64.getDecoder().wrap(bais)) {
int len;
byte[] bytes = new byte[100];
while ((len = is.read(bytes)) != -1) {
System.out.print(new String(bytes, 0, len, "utf-8"));
}
} catch (IOException ex) {
Logger.getLogger(Base64Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public class Base64Test {
Logger logger = Logger.getLogger(this.getClass().getName());
String charset_utf8 = "UTF-8";
private String srcStr = "壹贰叁肆伍陆柒捌玖拾佰仟万亿零整元角分";
private String srcUrl = "https://www.baidu.com/s?wd=jdk&rsv_page=1¶=中国";
@Test
public void basicTest() {
logger.info("basec test");
try {
byte[] enStrBytes = Base64.getEncoder().encode(srcStr.getBytes(Charset.forName(charset_utf8)));
String enStr = Base64.getEncoder().encodeToString(srcStr.getBytes(charset_utf8));
System.out.println(new String(enStrBytes, charset_utf8));
System.out.println(enStr);
byte[] deStrBytes = Base64.getDecoder().decode(enStrBytes);
byte[] deStrBytes1 = Base64.getDecoder().decode(enStr);
logger.info(new String(deStrBytes, charset_utf8));
logger.info(new String(deStrBytes1, charset_utf8));
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Base64Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Test
public void urlTest() {
logger.info("url test");
try {
byte[] enStrBytes = Base64.getUrlEncoder().encode(srcUrl.getBytes(charset_utf8));
String enStr = Base64.getUrlEncoder().encodeToString(srcUrl.getBytes(charset_utf8));
System.out.println(new String(enStrBytes, charset_utf8));
System.out.println(enStr);
byte[] deStrBytes = Base64.getUrlDecoder().decode(enStrBytes);
byte[] deStrBytes1 = Base64.getUrlDecoder().decode(enStr);
logger.info(new String(deStrBytes, charset_utf8));
logger.info(new String(deStrBytes1, charset_utf8));
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Base64Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Test
public void mimeTest() {
logger.info("mime test");
try {
byte[] toEncode = srcStr.getBytes(charset_utf8);
String mimeEncoded = Base64.getMimeEncoder().encodeToString(toEncode);
System.out.println(mimeEncoded);
String deMimeStr = new String(Base64.getMimeDecoder().decode(mimeEncoded), charset_utf8);
logger.info(deMimeStr);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Base64Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Test
public void wrapTest() {
logger.info("wrap test");
String src = "This is the content of any resource read from somewhere"
+ " into a stream. This can be text, image, video or any other stream.";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (OutputStream os = Base64.getEncoder().wrap(baos)) {
os.write(src.getBytes("utf-8"));
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Base64Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Base64Test.class.getName()).log(Level.SEVERE, null, ex);
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
try (InputStream is = Base64.getDecoder().wrap(bais)) {
int len;
byte[] bytes = new byte[100];
while ((len = is.read(bytes)) != -1) {
System.out.print(new String(bytes, 0, len, "utf-8"));
}
} catch (IOException ex) {
Logger.getLogger(Base64Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我看的觉得原理讲的比较清楚的文章:Base64编码原理与应用