Java中使用BASE64加密&解密

package com.bao.tools.encryption;

import java.io.IOException;

import org.junit.Test;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * @title BASE64加密&解密
 * @description
 * 邮件,HTTP协议,一般用户名及密码
 * @author Administrator
 * @date 2015-7-16
 */
public class CBase64 {
    /**
     * 加密
     * BASE64Encoder类
     */
    public static String setEncrypted(String value){
        if(value==null)return null;
        return new BASE64Encoder().encodeBuffer(value.getBytes());
    }
    /**
     * 解密
     * BASE64Decoder类
     * @throws IOException
     */
    public static String getEncrypted(String value) throws IOException{
        if(value==null)return null;
        BASE64Decoder d = new BASE64Decoder();
        byte[] b = d.decodeBuffer(value);
        return new String(b);
        
    }
    
    /*
     * 测试
     */
    @Test
    public void test() throws IOException {
        String name = "李u2";
        String hide = setEncrypted(name);
        String open = getEncrypted(hide);

        System.out.println("------BASE64编码与解码-------");
        System.out.println("元字符为:"+name);
        System.out.println("编 码 :"+hide);
        System.out.println("解 码 :"+open);                
    }
}

 

运行结果如下:

 

个人见解:该加密算法安全系数相对比较低,相对于要求安全度高的信息不建议采用该方法 。

posted @ 2015-07-16 11:15  rickbao  阅读(788)  评论(0编辑  收藏  举报