UrlBase64加密解密

本文介绍了UrlBase64加密解密的原理,与标准Base64的区别在于UrlBase64中使用“-”和“_”替代“+”和“/”,并避免使用“=”。举例展示了Bouncy Castle和Commons Codec两个库的实现,Bouncy Castle使用“.”作为填充符,而Commons Codec则不使用填充符。因此,对于URL地址中的加密,应使用UrlBase64编码而非Base64编码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.youkuaiyun.com/lonelyroamer/article/details/7638435

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值