原理:
Base64 使用US-ASCII子集的64个字符,即大小写的26个英文字母,0-9,+,/。
编码总是基于3个字符,每个字符用8位二进制表示,因此一共24位,再分为4四组,每组6位,表示一个Base64的值。如下:
"A", "B", "C", "D", "E", "F", "G", "H", "I","J", "K", "L", "M", "N", "O", "P",
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f",
"g", "h", "i","j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/"
Base64值为0就是A,为27的就是b。这样,每3个字符产生4位的Base64字符。如果被加密的字符串每3个一组,还剩1或2个字符,使用特殊字符"="补齐Base64的4字。
如,编码只有2个字符“me”,m的ascii是109,e的是101,用二进制表示分别是01101101、01100101,连接起来就是0110110101100101,再按6位分为一组:011011、010110、010100(不足6位补0),分别ascii分别是27、22、20,即Base64值为bWU,Base64不足4字,用=补齐,因此bWU=就me的Base64值。
用java的按位逻辑和移位运算就可以实现该算法。但实际上,并不用我们自己去编程实现。现有实现该加密解密算法的程序很多,如javamail的MimeUtility。
实践:javamail的MimeUtility实现对字符串的加密解密
第一步,在eclipse新建一个java项目,并引进javamail.jar
第二步,在com.mascot.encrypt包下创建测试单元,注意引进javax.mail.internet.MimeUtility
package com.mascot.encrypt;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import javax.mail.internet.MimeUtility;
public class Base64 {
public static BufferedReader decode(String b64string) throws Exception {
return new BufferedReader(
new InputStreamReader(
MimeUtility.decode(
new ByteArrayInputStream(
b64string.getBytes()), "base64")));
}
public static void main(String args[]) throws Exception{
System.out.println(encodeAsString("hello"));
System.out.println(decodeAsString("aGVsbG8="));
System.out.println(decodeAsString("aGVsbG8A"));
}
public static String decodeAsString(String b64string) throws Exception {
if (b64string == null) {
return b64string;
}
String returnString = decode(b64string).readLine();
if (returnString == null) {
return returnString;
}
return returnString.trim();
}
public static ByteArrayOutputStream encode(String plaintext)
throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] in = plaintext.getBytes();
ByteArrayOutputStream inStream = new ByteArrayOutputStream();
inStream.write(in, 0, in.length);
//补0
if ((in.length % 3 ) == 1){
inStream.write(0);
inStream.write(0);
} else if((in.length % 3 ) == 2){
inStream.write(0);
}
inStream.writeTo( MimeUtility.encode(out, "base64") );
return out;
}
public static String encodeAsString(String plaintext) throws Exception {
return encode(plaintext).toString();
}
}
第三步,运行程序,结果:
aGVsbG8A
hello
hello
注意到hello的加密结果并不是aGVsbG8=,而是aGVsbG8A,这是因为程序补齐都是补0,而0对应的Base64值就是A,这是我们程序实现与上述理论不同造成的。
Base64 使用US-ASCII子集的64个字符,即大小写的26个英文字母,0-9,+,/。
编码总是基于3个字符,每个字符用8位二进制表示,因此一共24位,再分为4四组,每组6位,表示一个Base64的值。如下:
"A", "B", "C", "D", "E", "F", "G", "H", "I","J", "K", "L", "M", "N", "O", "P",
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f",
"g", "h", "i","j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/"
Base64值为0就是A,为27的就是b。这样,每3个字符产生4位的Base64字符。如果被加密的字符串每3个一组,还剩1或2个字符,使用特殊字符"="补齐Base64的4字。
如,编码只有2个字符“me”,m的ascii是109,e的是101,用二进制表示分别是01101101、01100101,连接起来就是0110110101100101,再按6位分为一组:011011、010110、010100(不足6位补0),分别ascii分别是27、22、20,即Base64值为bWU,Base64不足4字,用=补齐,因此bWU=就me的Base64值。
用java的按位逻辑和移位运算就可以实现该算法。但实际上,并不用我们自己去编程实现。现有实现该加密解密算法的程序很多,如javamail的MimeUtility。
实践:javamail的MimeUtility实现对字符串的加密解密
第一步,在eclipse新建一个java项目,并引进javamail.jar
第二步,在com.mascot.encrypt包下创建测试单元,注意引进javax.mail.internet.MimeUtility
package com.mascot.encrypt;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import javax.mail.internet.MimeUtility;
public class Base64 {
public static BufferedReader decode(String b64string) throws Exception {
return new BufferedReader(
new InputStreamReader(
MimeUtility.decode(
new ByteArrayInputStream(
b64string.getBytes()), "base64")));
}
public static void main(String args[]) throws Exception{
System.out.println(encodeAsString("hello"));
System.out.println(decodeAsString("aGVsbG8="));
System.out.println(decodeAsString("aGVsbG8A"));
}
public static String decodeAsString(String b64string) throws Exception {
if (b64string == null) {
return b64string;
}
String returnString = decode(b64string).readLine();
if (returnString == null) {
return returnString;
}
return returnString.trim();
}
public static ByteArrayOutputStream encode(String plaintext)
throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] in = plaintext.getBytes();
ByteArrayOutputStream inStream = new ByteArrayOutputStream();
inStream.write(in, 0, in.length);
//补0
if ((in.length % 3 ) == 1){
inStream.write(0);
inStream.write(0);
} else if((in.length % 3 ) == 2){
inStream.write(0);
}
inStream.writeTo( MimeUtility.encode(out, "base64") );
return out;
}
public static String encodeAsString(String plaintext) throws Exception {
return encode(plaintext).toString();
}
}
第三步,运行程序,结果:
aGVsbG8A
hello
hello
注意到hello的加密结果并不是aGVsbG8=,而是aGVsbG8A,这是因为程序补齐都是补0,而0对应的Base64值就是A,这是我们程序实现与上述理论不同造成的。