zoukankan      html  css  js  c++  java
  • 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);                
        }
    }

    运行结果如下:

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

  • 相关阅读:
    Java Web Jsp EL ${ user.name }
    Java Web 网络留言板5 (javaBean技术)
    Java Web Web应用,打包和web.xml文件
    Java Web JSTL (实例)
    基于wpf的相关设计问题ViewModel
    c++ primer学习笔记(1)
    记上海俱乐部活动
    c++ primer学习笔记(3)字符串操作
    wpf&& silverlight的Behavior
    基于wpf的相关设计问题Command的使用
  • 原文地址:https://www.cnblogs.com/rick168/p/4650674.html
Copyright © 2011-2022 走看看