Base64 编码/解码介绍
Base64是一种基于64个可打印字符来表示二进制数据的表示方法。由于2的6次方等于64,所以每6个比特为一个单元,对应某个可打印字符。三个字节有24个比特,对应于4个Base64单元,即3个字节需要用4个可打印字符来表示。它可用来作为电子邮件的传输编码。在Base64中的可打印字符包括字母A-Z、a-z、数字0-9,这样共有62个字符,此外两个可打印符号在不同的系统中而不同。一些如uuencode的其他编码方法,和之后binhex的版本使用不同的64字符集来代表6个二进制数字,但是它们不叫Base64。
Base64常用于在通常处理文本数据的场合,表示、传输、存储一些二进制数据。包括MIME的email、在XML中存储复杂数据。
Base64索引表:
数值 | 字符 | 数值 | 字符 | 数值 | 字符 | 数值 | 字符 |
---|---|---|---|---|---|---|---|
0 | A | 16 | Q | 32 | g | 48 | w |
1 | B | 17 | R | 33 | h | 49 | x |
2 | C | 18 | S | 34 | i | 50 | y |
3 | D | 19 | T | 35 | j | 51 | z |
4 | E | 20 | U | 36 | k | 52 | 0 |
5 | F | 21 | V | 37 | l | 53 | 1 |
6 | G | 22 | W | 38 | m | 54 | 2 |
7 | H | 23 | X | 39 | n | 55 | 3 |
8 | I | 24 | Y | 40 | o | 56 | 4 |
9 | J | 25 | Z | 41 | p | 57 | 5 |
10 | K | 26 | a | 42 | q | 58 | 6 |
11 | L | 27 | b | 43 | r | 59 | 7 |
12 | M | 28 | c | 44 | s | 60 | 8 |
13 | N | 29 | d | 45 | t | 61 | 9 |
14 | O | 30 | e | 46 | u | 62 | + |
15 | P | 31 | f | 47 | v | 62 | / |
例子
在网上随便找了一段唯美的英文句子
Within you I lose myself, without you I find myself wanting to be lost again.
经过Base64编码以后成为:
V2l0aGluIHlvdSBJIGxvc2UgbXlzZWxmLCB3aXRob3V0IHlvdSBJIGZpbmQgbXlzZWxmIHdhbnRpbmcgdG8gYmUgbG9zdCBhZ2Fpbi4=
可以看出,编码后的句子比原本长了接近1/3.
java代码的实现
在java中,比较常用的有三种方式来进行Base64编码和解码,分别是利用jdk自带的实现类进行、Apache的Commons Codec工具类包实现、bouncy castle开源类包实现。
方式一
在JDK中,已经自带了可进行Base64编码和解码的类—–BASE64Encoder和BASE64Decoder,使用很方便。
package com.denylin.securitybase64;
import java.io.IOException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class JdkBase64demo {
public static void main(String[] args) {
String clearText = "Within you I lose myself, without you I find myself wanting to be lost again.";
String encode = jdkBase64Encode(clearText.getBytes());
System.out.println(encode);
String decode = jdkBase64Decode(encode);
System.out.println(decode);
}
/**
* jdk自带方式进行 Base64编码
* @param clearText 明文
* @return
*/
public static String jdkBase64Encode(byte[] clearText) {
BASE64Encoder encoder = new BASE64Encoder();
String encode = encoder.encode(clearText);
return encode;
}
/**
* jdk自带方式进行 Base64解码
* @param cipherText 密文
* @return
*/
public static String jdkBase64Decode(String cipherText) {
BASE64Decoder decoder = new BASE64Decoder();
byte[] decode = null;
try {
decode = decoder.decodeBuffer(cipherText);
} catch (IOException e) {
e.printStackTrace();
}
return new String(decode);
}
}
但有一点要注意,利用BASE64Encoder.encode()编码以后,在得到的密文中,每隔76个字符就会加上一个回车换行,这是因为BASE64Encoder遵循了RFC 822的规定,详情可自行查阅。
V2l0aGluIHlvdSBJIGxvc2UgbXlzZWxmLCB3aXRob3V0IHlvdSBJIGZpbmQgbXlzZWxmIHdhbnRp
bmcgdG8gYmUgbG9zdCBhZ2Fpbi4=
Within you I lose myself, without you I find myself wanting to be lost again.
方式二
利用Apache Commons Codec的Base64类实现Base64编码和解码。
package com.denylin.securitybase64;
import org.apache.commons.codec.binary.Base64;
public class CommonsCodesBase64demo {
public static void main(String[] args) {
String clearText = "Within you I lose myself, without you I find myself wanting to be lost again.";
byte[] encode = commonsCodesBase64Encoder(clearText.getBytes());
System.out.println(new String(encode));
byte[] decode = commonsCodesBase64Decoder(encode);
System.out.println(new String(decode));
}
/**
* commons codes 方式Base64编码
* @param clearText 明文
* @return
*/
public static byte[] commonsCodesBase64Encoder(byte[] clearText){
byte[] encode = Base64.encodeBase64(clearText);
return encode;
}
/**
* commons codes 方式Base64解码
* @param cipherText 密文
* @return
*/
public static byte[] commonsCodesBase64Decoder(byte[] cipherText){
byte[] decode = Base64.decodeBase64(cipherText);
return decode;
}
}
输出结果:
V2l0aGluIHlvdSBJIGxvc2UgbXlzZWxmLCB3aXRob3V0IHlvdSBJIGZpbmQgbXlzZWxmIHdhbnRpbmcgdG8gYmUgbG9zdCBhZ2Fpbi4=
Within you I lose myself, without you I find myself wanting to be lost again.
方式三
利用bouncy castle的Base64类进行Base64编码和解码。
而bouncy castle(轻量级密码术包)是一种用于 Java 平台的开放源码的轻量级密码术包。它支持大量的密码术算法,并提供 JCE 1.2.1 的实现。Bouncy Castle是轻量级的,从J2SE 1.4到J2ME(包括MIDP)平台,它都可以运行。它是在MIDP上运行的唯一完整的密码术包。
package com.denylin.securitybase64;
import org.bouncycastle.util.encoders.Base64;
public class BouncyCastleBase64demo {
public static void main(String[] args) {
String clearText = "Within you I lose myself, without you I find myself wanting to be lost again.";
byte[] encode = bouncyCastleBase64Encoder(clearText.getBytes());
System.out.println(new String(encode));
byte[] decode = bouncyCastleBase64Decoder(encode);
System.out.println(new String(decode));
}
/**
* bouncy castle 方式进行Base64加密
* @param clearText 明文
* @return
*/
public static byte[] bouncyCastleBase64Encoder(byte[] clearText){
byte[] encode = Base64.encode(clearText);
return encode;
}
/**
* bouncy castle 方式进行Base64解密
* @param cipherText 密文
* @return
*/
public static byte[] bouncyCastleBase64Decoder(byte[] cipherText){
byte[] decode = Base64.decode(cipherText);
return decode;
}
}
输出结果:
V2l0aGluIHlvdSBJIGxvc2UgbXlzZWxmLCB3aXRob3V0IHlvdSBJIGZpbmQgbXlzZWxmIHdhbnRpbmcgdG8gYmUgbG9zdCBhZ2Fpbi4=
Within you I lose myself, without you I find myself wanting to be lost again.
附件:
以上代码用到的类包:
commons-codec-1.10.jar
http://download.csdn.net/detail/dunylin/9666449
Bouncy Castle
http://download.csdn.net/detail/dunylin/9666472