利用Java实现Base64加解密

Base64 编码/解码介绍

Base64是一种基于64个可打印字符来表示二进制数据的表示方法。由于2的6次方等于64,所以每6个比特为一个单元,对应某个可打印字符。三个字节有24个比特,对应于4个Base64单元,即3个字节需要用4个可打印字符来表示。它可用来作为电子邮件的传输编码。在Base64中的可打印字符包括字母A-Z、a-z、数字0-9,这样共有62个字符,此外两个可打印符号在不同的系统中而不同。一些如uuencode的其他编码方法,和之后binhex的版本使用不同的64字符集来代表6个二进制数字,但是它们不叫Base64。

Base64常用于在通常处理文本数据的场合,表示、传输、存储一些二进制数据。包括MIME的email、在XML中存储复杂数据。

Base64索引表:

数值字符数值字符数值字符数值字符
0A16Q32g48w
1B17R33h49x
2C18S34i50y
3D19T35j51z
4E20U36k520
5F21V37l531
6G22W38m542
7H23X39n553
8I24Y40o564
9J25Z41p575
10K26a42q586
11L27b43r597
12M28c44s608
13N29d45t619
14O30e46u62+
15P31f47v62/

例子

在网上随便找了一段唯美的英文句子

Within you I lose myself, without you I find myself wanting to be lost again.

经过Base64编码以后成为:

V2l0aGluIHlvdSBJIGxvc2UgbXlzZWxmLCB3aXRob3V0IHlvdSBJIGZpbmQgbXlzZWxmIHdhbnRpbmcgdG8gYmUgbG9zdCBhZ2Fpbi4=

可以看出,编码后的句子比原本长了接近1/3.

java代码的实现

在java中,比较常用的有三种方式来进行Base64编码和解码,分别是利用jdk自带的实现类进行Apache的Commons Codec工具类包实现bouncy castle开源类包实现

方式一

在JDK中,已经自带了可进行Base64编码和解码的类—–BASE64Encoder和BASE64Decoder,使用很方便。

package com.denylin.securitybase64;

import java.io.IOException;

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

public class JdkBase64demo {
    public static void main(String[] args) {
        String clearText = "Within you I lose myself, without you I find myself wanting to be lost again.";
        String encode = jdkBase64Encode(clearText.getBytes());
        System.out.println(encode);
        String decode = jdkBase64Decode(encode);
        System.out.println(decode);
    }

    /**
     * jdk自带方式进行 Base64编码
     * @param clearText 明文
     * @return
     */
    public static String jdkBase64Encode(byte[] clearText) {
        BASE64Encoder encoder = new BASE64Encoder();
        String encode = encoder.encode(clearText);
        return encode;
    }

    /**
     * jdk自带方式进行 Base64解码
     * @param cipherText 密文
     * @return
     */
    public static String jdkBase64Decode(String cipherText) {
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] decode = null;
        try {
            decode = decoder.decodeBuffer(cipherText);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(decode);
    }
}

但有一点要注意,利用BASE64Encoder.encode()编码以后,在得到的密文中,每隔76个字符就会加上一个回车换行,这是因为BASE64Encoder遵循了RFC 822的规定,详情可自行查阅。

V2l0aGluIHlvdSBJIGxvc2UgbXlzZWxmLCB3aXRob3V0IHlvdSBJIGZpbmQgbXlzZWxmIHdhbnRp
bmcgdG8gYmUgbG9zdCBhZ2Fpbi4=
Within you I lose myself, without you I find myself wanting to be lost again.

方式二

利用Apache Commons Codec的Base64类实现Base64编码和解码。

package com.denylin.securitybase64;

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

public class CommonsCodesBase64demo {
    public static void main(String[] args) {
        String clearText = "Within you I lose myself, without you I find myself wanting to be lost again.";
        byte[] encode = commonsCodesBase64Encoder(clearText.getBytes());
        System.out.println(new String(encode));
        byte[] decode = commonsCodesBase64Decoder(encode);
        System.out.println(new String(decode));
    }

    /**
     * commons codes 方式Base64编码
     * @param clearText 明文
     * @return
     */
    public static byte[] commonsCodesBase64Encoder(byte[] clearText){
        byte[] encode = Base64.encodeBase64(clearText);
        return encode;
    }

    /**
     * commons codes 方式Base64解码
     * @param cipherText 密文
     * @return
     */
    public static byte[] commonsCodesBase64Decoder(byte[] cipherText){
        byte[] decode = Base64.decodeBase64(cipherText);
        return decode;
    }
}

输出结果:

V2l0aGluIHlvdSBJIGxvc2UgbXlzZWxmLCB3aXRob3V0IHlvdSBJIGZpbmQgbXlzZWxmIHdhbnRpbmcgdG8gYmUgbG9zdCBhZ2Fpbi4=
Within you I lose myself, without you I find myself wanting to be lost again.

方式三

利用bouncy castle的Base64类进行Base64编码和解码。

而bouncy castle(轻量级密码术包)是一种用于 Java 平台的开放源码的轻量级密码术包。它支持大量的密码术算法,并提供 JCE 1.2.1 的实现。Bouncy Castle是轻量级的,从J2SE 1.4到J2ME(包括MIDP)平台,它都可以运行。它是在MIDP上运行的唯一完整的密码术包。

package com.denylin.securitybase64;

import org.bouncycastle.util.encoders.Base64;

public class BouncyCastleBase64demo {
    public static void main(String[] args) {
        String clearText = "Within you I lose myself, without you I find myself wanting to be lost again.";
        byte[] encode = bouncyCastleBase64Encoder(clearText.getBytes());
        System.out.println(new String(encode));
        byte[] decode = bouncyCastleBase64Decoder(encode);
        System.out.println(new String(decode));
    }

    /**
     * bouncy castle 方式进行Base64加密
     * @param clearText 明文
     * @return
     */
    public static byte[] bouncyCastleBase64Encoder(byte[] clearText){
        byte[] encode = Base64.encode(clearText);
        return encode;
    }

    /**
     * bouncy castle 方式进行Base64解密
     * @param cipherText 密文
     * @return
     */
    public static byte[] bouncyCastleBase64Decoder(byte[] cipherText){
        byte[] decode = Base64.decode(cipherText);
        return decode;
    }
}

输出结果:

V2l0aGluIHlvdSBJIGxvc2UgbXlzZWxmLCB3aXRob3V0IHlvdSBJIGZpbmQgbXlzZWxmIHdhbnRpbmcgdG8gYmUgbG9zdCBhZ2Fpbi4=
Within you I lose myself, without you I find myself wanting to be lost again.

附件:
以上代码用到的类包:
commons-codec-1.10.jar
http://download.csdn.net/detail/dunylin/9666449

Bouncy Castle
http://download.csdn.net/detail/dunylin/9666472

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值