Base64字符映射表:
上面的是Base64的字符映射表。
URL Base64的某些方面有別於Base64,它不需要定義每行字符數及行末回車換行符。同時,根據URL相關要求,符號“+”和符號“/”是不允許出現在URL中的,於是採用“-”和“_”符號取代。也就是說在上圖的Base64字符映射表中 Value 63 對應的Encoding變成了“-”,Value 63 對應的Encoding變成了“_”。
同樣,在URL中,符號“=”用作參數分隔符,所以也是不合法的。“=”在Base64中用作填充符,如果需要定長的Base64編碼串,也姐需要相應的代替符號。Bouncy Castle和Commons Codec都實現了UrlBase64算法,不同的是Bouncy Castle使用“.”作爲填充符,而Commons Codec直接放棄了填充符,使用不定長UrlBase64編碼。
1、Bouncy Castle的實現和應用
import java.io.UnsupportedEncodingException;
import org.bouncycastle.util.encoders.UrlBase64;
/**
* 封裝Base64的工具類
*
*/
class UrlBase64Coder2{
public final static String ENCODING="UTF-8";
//加密
public static String encoded(String data) throws UnsupportedEncodingException{
byte[] b=UrlBase64.encode(data.getBytes(ENCODING));
return new String(b,ENCODING);
}
//解密
public static String decode(String data) throws UnsupportedEncodingException{
byte[] b=UrlBase64.decode(data.getBytes(ENCODING));
return new String(b,ENCODING);
}
}
/**
* 測試類
*/
public class test05 {
public static void main(String[] args) throws UnsupportedEncodingException {
String str="some string";
//加密該字符串
String encodedString=UrlBase64Coder2.encoded(str);
System.out.println(encodedString);
//解密該字符串
String decodedString=UrlBase64Coder2.decode(encodedString);
System.out.println(decodedString);
}
}
輸出:
c29tZSBzdHJpbmc.
some string
2、Commons Codec的實現和應用
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;
/**
* 封裝Base64的工具類
*
*/
class UrlBase64Coder {
public final static String ENCODING = "UTF-8";
// 加密
public static String encoded(String data) throws UnsupportedEncodingException {
byte[] b = Base64.encodeBase64URLSafe(data.getBytes(ENCODING));
return new String(b, ENCODING);
}
// 解密
public static String decode(String data) throws UnsupportedEncodingException {
byte[] b = Base64.decodeBase64(data.getBytes(ENCODING));
return new String(b, ENCODING);
}
}
/**
* 測試類
*/
public class test06 {
public static void main(String[] args) throws UnsupportedEncodingException {
String str = "some string";
// 加密該字符串
String encodedString = UrlBase64Coder.encoded(str);
System.out.println(encodedString);
// 解密該字符串
String decodedString = UrlBase64Coder.decode(encodedString);
System.out.println(decodedString);
}
}
輸出結果:
c29tZSBzdHJpbmc
some string
3、相比較Base64輸出
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;
/**
* 封裝Base64的工具類
*
*/
class UrlBase64Coder3{
public final static String ENCODING="UTF-8";
//加密
public static String encoded(String data) throws UnsupportedEncodingException {
// byte[] b=Base64.encodeBase64(binaryData)(data);
byte[] b=Base64.encodeBase64(data.getBytes(ENCODING));
return new String(b,ENCODING);
}
//解密
public static String decode(String data) throws UnsupportedEncodingException{
byte[] b=Base64.decodeBase64(data.getBytes(ENCODING));
return new String(b,ENCODING);
}
}
public class test07 {
public static void main(String[] args) throws UnsupportedEncodingException {
String str="some string";
//加密該字符串
String encodedString=UrlBase64Coder3.encoded(str);
System.out.println(encodedString);
//解密該字符串
String decodedString=UrlBase64Coder3.decode(encodedString);
System.out.println(decodedString);
}
}
輸出結果:
c29tZSBzdHJpbmc=
some string
所以,在URL地址中進行加密不能使用Base64編碼,要使用UrlBase64編碼
參考文章:
http://blog.csdn.net/lonelyroamer/article/details/7638435