模拟文件的加密与解密

import java.io.*;
import java.util.*;
class inFile{
String passNum = "abcdefg";
int j = 0;
int i = 0;
public void fileEncription() {
try{
FileInputStream fis = new FileInputStream("C:\\Users\\pc\\Desktop\\1.txt");
FileOutputStream fos = new FileOutputStream("C:\\Users\\pc\\Desktop\\2.txt");
int b;
while((b = fis.read()) != -1) {
b = (int) (b ^ 7);
if(j == passNum.length()) {
j = 0;
}
b = b + passNum.charAt(j);
j++;
fos.write(b);
}
fis.close();
fos.close();
System.out.println("加密完成!");
}catch(IOException e) {
System.out.println("找不到指定文件!");
}

}
public void InEncryption(){
try{
FileInputStream fis1 = new FileInputStream("C:\\Users\\pc\\Desktop\\2.txt");
FileOutputStream fos1 = new FileOutputStream("C:\\Users\\pc\\Desktop\\3.txt");
int b;
while((b = fis1.read()) != -1) {
if(i == passNum.length()) {
i = 0;
}
b = b - passNum.charAt(i);
b = (int) (b ^ 7);

i++;
fos1.write(b);
}
fis1.close();
fos1.close();
System.out.println("解密完成!");
}catch(Exception e){
System.out.println("找不到指定文件!");
}
}
}
public class Encryption extends inFile{
public static void main(String args[]) {
inFile in = new inFile();
System.out.println("1:加密");
System.out.println("2:解密");
int n = new Scanner(System.in).nextInt();
if(n == 1) {
in.fileEncription();
}else if(n == 2) {
in.InEncryption();
}else {
System.out.println("输入错误!");
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值