铁马冰河2000

导航

统计

Java加解密-Base64算法

base64算法是基于64个字符的一种替换算法。

base64加密的产生式电子邮件的“历史问题”——邮件只能传输ASCII码。

base64加密的应用场景:email、密钥、证书文件。

该算法可以由3种方式实现:JDK、Bouncy Castle、Commons Codec。

============================================================================Base64加解密工具类:

复制代码
import org.apache.commons.codec.binary.Base64;

public class SecurityBASE64 {
    
    /**
     * base64算法是基于64个字符的一种替换算法。base64加密的产生式电子邮件的“历史问题”——邮件只能传输ASCII码。
     * base64加密的应用场景:email、密钥、证书文件。
     *  该算法可以由2种方式实现:Bouncy Castle、Commons Codec
     */
    
    /** 
     * Commons Codec实现base64编码 
     */
    public static String encodeCC(String src) {
        byte[] encode = Base64.encodeBase64(src.getBytes());
        return new String(encode);
    }
    public static String decodeCC(String src) {
        byte[] decode = Base64.decodeBase64(src);
        return new String(decode);
    }
    
    /**
     * Bouncy Castle实现base64编码 
     */
    public static String encodeBC(String src) {
        byte[] encode = org.bouncycastle.util.encoders.Base64.encode(src.getBytes());
        return new String(encode);
    }
    public static String decodeBC(String src) {
        byte[] decode = org.bouncycastle.util.encoders.Base64.decode(src);
        return new String(decode);
    }
}
复制代码

============================================================================Base64加解密工具测试类:

复制代码
    /**
     * BASE64加密和解密
     */
    @Test
    public void test_base64() {
        String inStr = "面向对象编程,object-oriented!@#*5";
        String encodeCC = SecurityBASE64.encodeCC(inStr);
        System.out.println("Commons Codec实现base64编码:\t" + encodeCC);
        String decodeCC = SecurityBASE64.decodeCC(encodeCC);
        System.out.println("Commons Codec实现base64解码:\t" + decodeCC);
        String encodeBC = SecurityBASE64.encodeBC(inStr);
        System.out.println("Bouncy Castle实现base64编码:\t" + encodeBC);
        String decodeBC = SecurityBASE64.decodeBC(encodeBC);
        System.out.println("Bouncy Castle实现base64解码:\t" + decodeBC);
    }
复制代码

 

posted on   铁马冰河2000  阅读(441)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示