使用base64进行加密解密的方法
1.创建一个javaProject的项目
2.导入这三个jar包到项目工程中
三个jar包的文件点击这里链接: https://pan.baidu.com/s/1CfukDRgF2Whe22M7Wu_nRQ 密码: xm47
3.右键点击jar包文件,选择Build Path,选择 add to build path 把jar包添加进去!
添加成功后就是这个样子
5.检查项目中是否有JUnit的包文件,如果有就忽略此步骤,没有的导入JUnit的包.因为后面要用到 @Test !
导入JUnit包的方法:右击工程,选择Build path→Configure Build path,然后在Library中点击右边的Add Library,选择JUnit,下一步,选择JUnit4,点击OK即可。步骤图如下:
选择JUnit
注意选择JUnit4
如图导入成功
6.开始完成代码部分,首先创建一个demol的类文件,@Test就是需要导入刚刚的JUnit的包文件!
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25import java.io.UnsupportedEncodingException; import org.apache.commons.codec.binary.Base64; import org.junit.Test; public class demol { @Test public void funl() throws UnsupportedEncodingException{ //加密 String str = "董老师你好吗?我挺好的呢!"; byte[] bytes = str.getBytes("utf-8"); bytes = Base64.encodeBase64(bytes); str = new String(bytes,"utf-8"); System.out.println(str); //解密 bytes = str.getBytes("utf-8"); bytes = Base64.decodeBase64(bytes); str = new String(bytes,"utf-8"); System.out.println(str); } });
7.运行代码
第一行就是加密后的效果,第二行就是解密后的效果!
最后
以上就是无情母鸡最近收集整理的关于java加密编码解密编码之Base64的全部内容,更多相关java加密编码解密编码之Base64内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复