Java上进行Base64的编码(Encode)和解码(Decode)

技术教程 · 2020-05-22

自己的一个小工具需要用到Base64加解密,因为用的还是java6,因此没有用上java8自带的base64,

搜索了一轮发现 commons-codec-1.10.jar包灰常靠谱,有需要的可以参考下

package com.base64;

import java.io.UnsupportedEncodingException;

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



public class Base64Test {

    
    public static void main(String[] args) throws UnsupportedEncodingException {
        // TODO Auto-generated method stub
        Base64 base64 = new Base64();
        String testContent= "5rWL6K+V55qE5Yqg5a+G5YaF5ZCM";
        
        //测试解密
        String decodeTestContent="";
        try {
            decodeTestContent= new String(base64.decode(testContent), "UTF-8");
            //System.out.print(decodeTestContent);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
        //测试加密
        byte[] testContentv2rayByte = decodeTestContent.getBytes("UTF-8");
        String encodeTestContentv2rayByte  = base64.encodeToString(testContentv2rayByte );
        System.out.println(encodeTestContentv2rayByte);
        
    }

}

官网下载地址:https://commons.apache.org/proper/commons-crypto/download_crypto.cgi

蓝奏云盘下载:密码:a7wp(https://busy.lanzous.com/b02yynzwb

Theme Jasmine by Kent Liao