标签:AC key 文件 system.in 字节 highlight 读取 失败 文件加密
System.out.println("文件:");
String s = new Scanner(System.in).nextLine();
File file = new File(s);
if(! file.isFile()) {
System.out.println("不是文件");
return;
}
System.out.print("KEY:");
int key = new Scanner(System.in).nextInt();
try {
encrypt(file, key);
System.out.println("完成");
} catch (Exception e) {
System.out.println("失败");
}
}
private static void encrypt(
File file, int key) throws Exception{
RandomAccessFile raf =
new RandomAccessFile(file, "rw");
//单字节读取标准格式
//int b;
//while((b = raf.read()) != -1) {
// b ^= key; //b = b^key;
// raf.seek(raf.getFilePointer()-1);
// raf.write(b);
//}
// 8k 8192
byte[] buff = new byte[8192];
int n;//保存一批的数量
while((n = raf.read(buff)) != -1) {
//数组中前n个字节加密
for(int i=0;i<n;i++) {
buff[i] ^= key;
}
//下标移回n个位置
raf.seek(raf.getFilePointer()-n);
//输出数组中前 n 个字节
raf.write(buff,0,n);
}
raf.close();
}
标签:AC key 文件 system.in 字节 highlight 读取 失败 文件加密
原文地址:https://www.cnblogs.com/xiaokaivip/p/9240174.html