java使用异或对文件进行加密解密
更新时间:2019年03月22日 09:47:16 作者:百无1用是书生
这篇文章主要为大家详细介绍了java使用异或方式对文件进行加密解密,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了java使用异或对文件进行加密解密的具体代码,供大家参考,具体内容如下
1.使用异或的方式加密文件的原理
一个数异或另一个数两次,结果一定是其本身
2.使用异或的原理加密文件
/**
* 将文件内容加密
* 使用异或的方式将a.txt加密复制出一个b.txt,放到同一个文件夹下
*/
@Test
public void encryptFile(){
FileInputStream in = null;
FileOutputStream out = null;
try {
String sourceFileUrl = "C:\\Users\\admin\\Desktop\\testIO\\a.txt";
String targetFileUrl = "C:\\Users\\admin\\Desktop\\testIO\\b.txt";
in = new FileInputStream(sourceFileUrl);
out = new FileOutputStream(targetFileUrl);
int data = 0;
while ((data=in.read())!=-1){
//将读取到的字节异或上一个数,加密输出
out.write(data^1234);
}
}catch (Exception e){
e.printStackTrace();
}finally {
//在finally中关闭开启的流
if (in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.使用异或的原理解密文件
/**
* 将文件内容解密
* 将使用异或的方式加密复制出的b.txt解密到c.txt,放到同一个文件夹下
*/
@Test
public void decryptFile(){
FileInputStream in = null;
FileOutputStream out = null;
try {
String sourceFileUrl = "C:\\Users\\admin\\Desktop\\testIO\\b.txt";
String targetFileUrl = "C:\\Users\\admin\\Desktop\\testIO\\c.txt";
in = new FileInputStream(sourceFileUrl);
out = new FileOutputStream(targetFileUrl);
int data = 0;
while ((data=in.read())!=-1){
//将读取到的字节异或上一个数,加密输出
out.write(data^1234);
}
}catch (Exception e){
e.printStackTrace();
}finally {
//在finally中关闭开启的流
if (in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
一次"java:程序包org.aspectj.lang不存在"问题解决实战记录
这篇文章主要给大家介绍了一次"java:程序包org.aspectj.lang不存在"问题解决的实战过程,这个错误提示意味着你的Java程序中引用了org.aspectj.lang这个包,但是该包并不存在,文章通过图文介绍的非常详细,需要的朋友可以参考下2023-06-06
SpringBoot favicon Chrome设置问题解决方案
在本篇文章里小编给大家分享的是关于SpringBoot favicon Chrome设置问题实例内容,小的朋友们可以参考学习下。2020-02-02
浅析springcloud 整合 zipkin-server 内存日志监控
Zipkin是一款开源的分布式实时数据追踪系统(Distributed Tracking System),其主要功能是聚集来自各个异构系统的实时监控数据。这篇文章主要介绍了springcloud 整合 zipkin-server 内存日志监控,需要的朋友可以参考下2019-11-11
SpringBoot+MybatisPlus+Mysql+JSP实战
这篇文章主要介绍了SpringBoot+MybatisPlus+Mysql+JSP实战,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-12-12


最新评论