使用Apache commons-codec Base64实现加解密(转)

来源:互联网 发布:mac删除键 编辑:程序博客网 时间:2024/05/16 19:28

commons-codec是Apache下面的一个加解密开发包

官方地址为:http://commons.apache.org/codec/

官方下载地址:http://commons.apache.org/codec/download_codec.cgi

在线文档:http://commons.apache.org/codec/userguide.html

下面示例为使用Base64实现字符串的加解密:


/**

     * 
     * 创建日期2011-4-25上午10:12:38
     * 修改日期
     * 作者:dh *TODO 使用Base64加密算法加密字符串
     *return
     */
    public static String encodeStr(String plainText){
        byte[] b=plainText.getBytes();
        Base64 base64=new Base64();
        b=base64.encode(b);
        String s=new String(b);
        return s;
    }
    
    /**
     * 
     * 创建日期2011-4-25上午10:15:11
     * 修改日期
     * 作者:dh     *TODO 使用Base64加密
     *return
     */
    public static String decodeStr(String encodeStr){
        byte[] b=encodeStr.getBytes();
        Base64 base64=new Base64();
        b=base64.decode(b);
        String s=new String(b);
        return s;
    }
0 0
原创粉丝点击