灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2880回复:0

java加密解密研究4、UrlBase64算法

楼主#
更多 发布于:2012-09-08 09:45

先贴出Base64字符映射表:

49_3710_13b9d4d9b19775f.png[设为封面] [删除]
上面的是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的实现和应用

package Test;

import java.io.UnsupportedEncodingException;

import org.bouncycastle.util.encoders.UrlBase64;
/**
* 封装Base64的工具类
*
*/
class UrlBase64Coder{
    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 UrlBase64Test {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str="Ad31";
        //加密该字符串  
        String encodedString=UrlBase64Coder.encoded(str);
        System.out.println(encodedString);
        //解密该字符串  
        String decodedString=UrlBase64Coder.decode(encodedString);
        System.out.println(decodedString);
    }
}
package Test;
import java.io.UnsupportedEncodingException;
import org.bouncycastle.util.encoders.UrlBase64;
/**
* 封装Base64的工具类
*
*/
class UrlBase64Coder{
public final static String ENCODING="UTF-8";
//加密 www.atcpu.com
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 UrlBase64Test {
public static void main(String[] args) throws UnsupportedEncodingException {
  String str="Ad31";
  //加密该字符串
  String encodedString=UrlBase64Coder.encoded(str);
  System.out.println(encodedString);
  //解密该字符串
  String decodedString=UrlBase64Coder.decode(encodedString);
  System.out.println(decodedString);
}
}




2、Commons Codec的实现和应用
[java] package Test;

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 UrlBase64Test {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "Ad31";
        // 加密该字符串  
        String encodedString = UrlBase64Coder.encoded(str);
        System.out.println(encodedString);
        // 解密该字符串  
        String decodedString = UrlBase64Coder.decode(encodedString);
        System.out.println(decodedString);
        
        
    }
}
package Test;
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 UrlBase64Test {
public static void main(String[] args) throws UnsupportedEncodingException {
  String str = "Ad31";
  // 加密该字符串
  String encodedString = UrlBase64Coder.encoded(str);
  System.out.println(encodedString);
  // 解密该字符串
  String decodedString = UrlBase64Coder.decode(encodedString);
  System.out.println(decodedString);
  
  
}
}


摘自 LonelyRoamer的专栏


喜欢0 评分0
游客

返回顶部