加解密---Base64

1、算法实现:

    1.1  JDK提供;     

package com.exiuge.mytest;

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

public class JdkBase64 {

    private static String src="hello,rose";

    public static void main(String[] args)throws Exception{
        endeCode(src);
    }

    public static void endeCode(String src)throws Exception{
        BASE64Encoder base64Encoder=new BASE64Encoder();
        String des=base64Encoder.encode(src.getBytes());
        System.out.println(des);

        BASE64Decoder base64Decoder=new BASE64Decoder();
        String deSrc=new String(base64Decoder.decodeBuffer(des));
        System.out.println(deSrc);
    }
}

 

    1.2  Commons Codec提供;    

package com.exiuge.mytest;

import org.apache.commons.codec.binary.Base64;

public class CommonsCodecBase64 {

    private static String src="hello,rose";

    public static void main(String[] args){
        enDeCode(src);
    }

    public static void enDeCode(String src){
        String des=new String(Base64.encodeBase64(src.getBytes()));
        System.out.println(des);

        String deSrc=new String(Base64.decodeBase64(des));
        System.out.println(deSrc);
    }
}

 

    1.3  Bouncy  Castle提供:

package com.exiuge.mytest;

import org.bouncycastle.util.encoders.Base64;

public class BouncyCastleBase64 {

    private static String src="hello,rose";

    public static void main(String[] args){
        enDeCode(src);
    }

    public static void enDeCode(String src){
        String des=new String(Base64.encode(src.getBytes()));
        System.out.println(des);

        String deSrc=new String(Base64.decode(des));
        System.out.println(deSrc);
    }
}

 2、应用场景

    e-mail、秘钥、证书文件

3、Base64算法产生原因

    邮件的“历史问题”

4、Base64算法定义

    基于64个字符的编码算法

  

posted on 2019-02-19 22:48  anpeiyong  阅读(119)  评论(0编辑  收藏  举报

导航