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