java Base64加密实现

Base64是网络上最常见的用于传输的8bit字节代码的编码方式,也常将把需要的二进制数据编码放在url中的形式上传,由于可读性差,所编码的数据不会被直接看到信息详情!

Base64 是采用字符替换的方式,将3个8位字节(3*8=24)转换为4个6位字节(4*64),之后再6位的前面补两个0,形成8位一个字节的形式,如果剩下的字符不足3个字节,则用0填充,输出字符使用'=',因此编码后输出的文本末尾可能出现1或者2个'='

Base64编码表:

java Base64加密实现_第1张图片

在java 中Base64的实现有3中

1:JDK

2:Commons codec

3:bouncy castle


commons codec架包下载地址

bouncy castle架包下载地址


实例1:JDK 方式实现Base64

package com.example.encryption;

import java.io.IOException;
import java.util.Base64;

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

/**
 * @author xuanyouwu
 * @email [email protected]
 * @time 2016-05-15 10:31
 */
public class Base64Test {
    private static String SrcData = "xuanyouwu";

    public static void log(String s) {
        System.out.println("------>" + s);
    }

    public static void main(String[] args) throws Exception {
        log("jdk base64Encode:" + jdkBas64Encode(SrcData));
        log("jdk base64Decode:" + jdkBas64Decode(jdkBas64Encode(SrcData)));

        log("jdk base64Encode2:" + jdkBas64Encode2(SrcData));
        log("jdk base64Decode2:" + jdkBas64Decode2(jdkBas64Encode2(SrcData)));
    }

    // jdk base64加密 sun.misc
    private static String jdkBas64Encode(String src) {
        BASE64Encoder base64Encoder = new BASE64Encoder();
        return base64Encoder.encode(src.getBytes());
    }

    // jdk base64解秘 sun.misc
    private static String jdkBas64Decode(String encodeData) {
        BASE64Decoder base64Decoder = new BASE64Decoder();
        try {
            return new String(base64Decoder.decodeBuffer(encodeData));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    // jdk base64加密 jdk util
    private static String jdkBas64Encode2(String src) {
        return Base64.getEncoder().encodeToString(src.getBytes());
    }

    // jdk base64揭秘 jdk util
    private static String jdkBas64Decode2(String encodeData) {
        return new String(Base64.getDecoder().decode(encodeData));
    }
}

运行结果:

------>jdk base64Encode:eHVhbnlvdXd1
------>jdk base64Decode:xuanyouwu
------>jdk base64Encode2:eHVhbnlvdXd1
------>jdk base64Decode2:xuanyouwu


实例2:commons codec实现Base64

java Base64加密实现_第2张图片


package com.example.encryption;

import java.io.IOException;
import java.util.Base64;

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

/**
 * @author xuanyouwu
 * @email [email protected]
 * @time 2016-05-15 10:31
 */
public class Base64Test {
    private static String SrcData = "xuanyouwu";

    public static void log(String s) {
        System.out.println("------>" + s);
    }

    public static void main(String[] args) throws Exception {
        log("cc base64Encode:" + ccBas64Encode(SrcData));
        log("cc base64Decode:" + ccBas64Decode(ccBas64Encode(SrcData)));
    }

    // commons.codec base64 加密
    private static String ccBas64Encode(String src) {
        return org.apache.commons.codec.binary.Base64.encodeBase64String(src.getBytes());
    }

    // commons.codec base64 解密
    private static String ccBas64Decode(String encodeData) {
        return new String(org.apache.commons.codec.binary.Base64.decodeBase64(encodeData.getBytes()));
    }
}


运行结果:

------>cc base64Encode:eHVhbnlvdXd1
------>cc base64Decode:xuanyouwu

实例3:Bouncy castle实现Base64:

package com.example.encryption;

import java.io.IOException;
import java.util.Base64;

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

/**
 * @author xuanyouwu
 * @email [email protected]
 * @time 2016-05-15 10:31
 */
public class Base64Test {
    private static String SrcData = "xuanyouwu";

    public static void log(String s) {
        System.out.println("------>" + s);
    }

    public static void main(String[] args) throws Exception {
        log("bc base64Encode:" + bcBase64Encode(SrcData));
        log("bc base64Decode:" + bcBase64Decode(bcBase64Encode(SrcData)));
    }


    // bouncy castle base64 加密
    private static String bcBase64Encode(String src) {
        return new String(org.bouncycastle.util.encoders.Base64.encode(src.getBytes()));
    }

    // bouncy castle base64 解密
    private static String bcBase64Decode(String encodeData) {
        return new String(org.bouncycastle.util.encoders.Base64.decode(encodeData.getBytes()));
    }
}


运行结果:

------>bc base64Encode:eHVhbnlvdXd1
------>bc base64Decode:xuanyouwu

你可能感兴趣的:(java_其他)