admin管理员组文章数量:1589660
文档加密
import java.io.*;
public class ToSecret {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("你想加密的文件名称Secret1.txt");
FileOutputStream fos= new FileOutputStream("你想加密之后的文件名称Secret2.txt");
int ch;
System.out.println("请输入密码:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String key = br.readLine();
while((ch=fis.read())!=-1){
ch=ch^Integer.valueOf(key);
fos.write(ch);
}
}
}
文档解密(只要两次输入的密钥相同就可以啦)
import java.io.*;
public class ToSecret {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("你想加密的文件名称Secret2.txt");
FileOutputStream fos= new FileOutputStream("你想加密之后的文件名称Secret3.txt");
int ch;
System.out.println("请输入密码:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String key = br.readLine();
while((ch=fis.read())!=-1){
ch=ch^Integer.valueOf(key);
fos.write(ch);
}
}
}
下面是效果图哦
Secret1.txt:
Secret2.txt
Secret3.txt
通过以上方法就简单的实现了文档的加密,是不是好神奇的样子哈哈哈~~~
其实我们只是利用了一个 简单的位运算符 " ^ " 而已,因为异或运算符与同一个变量异或两次等于它本身~~~小伙伴可以自己试一下
版权声明:本文标题:用java实现文档加密解密 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1728074205a1144276.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论