Java 实现 Base64 加解密

Java 实现 Base64 加解密

3.1 apache.commons-codex 包

apache.commons-codex 包中提供了的 Base64 等许多编码格式转换,可以通过其进行实现。

import org.apache.commons.codec.binary.Base64;
public class Base64Util {

    //base64 编码
    public static String encode(byte[] bytes) {
        return new String(Base64.encodeBase64(bytes));
    }
    
    //base64 解码
    public static String decode(byte[] bytes) {
        return new String(Base64.decodeBase64(bytes));
    }
    
    public static void main(String[] args) {
        String string = "test1234";
        //编码
        String encode = encode(string.getBytes());
        System.out.println(string + "\t编码后的字符串为:" + encode);
        //解码
        String decode = decode(encode.getBytes());
        System.out.println(encode + "\t字符串解码后为:" + decode);
    }
}
复制代码
posted @ 2022-08-11 15:55  BBS_自律  阅读(633)  评论(0)    收藏  举报