Java实现的Base64加密算法示例
作者:chengqiuming 时间:2023-10-29 00:37:08
本文实例讲述了Java实现的Base64加密算法。分享给大家供大家参考,具体如下:
一 算法实现
1、JDK
2、Commonc Codec
3、Bouncy Castle
二 代码
package com.imooc.security.base64;
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class ImoocBase64 {
private static String src = "cakin24 security base64";
public static void main(String[] args) {
jdkBase64();
commonsCodesBase64();
bouncyCastleBase64();
}
public static void jdkBase64() {
try {
BASE64Encoder encoder = new BASE64Encoder();
String encode = encoder.encode(src.getBytes());
System.out.println("encode : " + encode);
BASE64Decoder decoder = new BASE64Decoder();
System.out.println("decode : " + new String(decoder.decodeBuffer(encode)));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void commonsCodesBase64() {
byte[] encodeBytes = Base64.encodeBase64(src.getBytes());
System.out.println("encode : " + new String(encodeBytes));
byte[] decodeBytes = Base64.decodeBase64(encodeBytes);
System.out.println("decode : " + new String(decodeBytes));
}
public static void bouncyCastleBase64() {
byte[] encodeBytes = org.bouncycastle.util.encoders.Base64.encode(src.getBytes());
System.out.println("encode : " + new String(encodeBytes));
byte[] decodeBytes = org.bouncycastle.util.encoders.Base64.decode(encodeBytes);
System.out.println("decode : " + new String(decodeBytes));
}
}
三 运行效果
encode : Y2FraW4yNCBzZWN1cml0eSBiYXNlNjQ=
decode : cakin24 security base64
encode : Y2FraW4yNCBzZWN1cml0eSBiYXNlNjQ=
decode : cakin24 security base64
encode : Y2FraW4yNCBzZWN1cml0eSBiYXNlNjQ=
decode : cakin24 security base64
四 应用场景
email、秘钥、证书文件
五 产生原因
邮件的历史问题
六 其他
基于64个字符的编码算法,定义于RFC 2045
补充:这里使用到了sun.misc.BASE64Encoder
和sun.misc.BASE64Decoder
,可通过如下设置在Eclipse中使用:
右击项目 --> Properties --> Java Build Path --> 点开JRE System Library --> 点击Access rules --> Edit --> Add --> Resolution选择Accessible --> Rule Pattern填上 ** --> OK
中文版Eclipse设置如下图所示:
来源:https://blog.csdn.net/chengqiuming/article/details/78602205
标签:Java,Base64,加密算法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringMVC拦截器配置及运行流程解析
2023-03-30 15:38:57
![](https://img.aspxhome.com/file/2023/9/114049_0s.png)
JAVA集合框架工具类自定义Collections集合方法
2022-01-11 16:24:13
C#判断系统是32位还是64位的方法
2022-04-01 12:44:04
![](https://img.aspxhome.com/file/2023/9/108799_0s.png)
Java读取、写入文件如何解决乱码问题
2023-08-26 11:16:00
C#实现DataTable映射成Model的方法(附源码)
2023-03-12 06:10:56
剑指Offer之Java算法习题精讲求和篇
2022-04-07 14:05:36
![](https://img.aspxhome.com/file/2023/2/128822_0s.png)
Android编程实现滑动按钮功能详解
2022-04-14 04:58:39
![](https://img.aspxhome.com/file/2023/7/108317_0s.png)
Android LayoutInflater.inflate源码分析
2022-06-06 17:02:05
Java基础之String类使用与字符串比较
2023-11-09 23:46:51
Android中GPS定位的用法实例
2021-07-26 23:26:29
![](https://img.aspxhome.com/file/2023/0/137270_0s.png)
Java实现商城订单超时取消功能
2023-09-17 06:20:42
SpringBoot设置静态资源访问控制和封装集成方案
2021-08-23 19:08:44
![](https://img.aspxhome.com/file/2023/8/108408_0s.png)
深入讲解SpringBoot Actuator是什么
2022-08-21 13:44:09
![](https://img.aspxhome.com/file/2023/9/130009_0s.jpg)
使用HandlerMethodArgumentResolver用于统一获取当前登录用户
2023-04-20 12:09:16
Android音乐播放器制作 加入控制台(三)
2022-04-15 05:09:30
![](https://img.aspxhome.com/file/2023/1/90151_0s.gif)
详解spring security之httpSecurity使用示例
2023-08-03 09:46:17
Java常见问题之javac Hello.java找不到文件的解决方法
2023-12-15 21:19:45
![](https://img.aspxhome.com/file/2023/3/83163_0s.png)
C# 禁用鼠标中间键的方法
2022-01-24 04:17:24
Java实现走迷宫回溯算法
2022-06-02 05:11:29
![](https://img.aspxhome.com/file/2023/6/62976_0s.jpg)
C#利用TreeView控件实现目录跳转
2021-07-26 02:57:50
![](https://img.aspxhome.com/file/2023/6/89866_0s.png)