我有一个需要使用以下代码解密的文本文件:
//Rot13加解密 public class Rot13 {
private char [] letter = {' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
private int index = 0;
public String encrypt(String s){
String str = "";
//forloop to get the each character from the passing string
for(int i=0; i<s.length(); i++){
char c = Character.toUpperCase(s.charAt(i));
if(c == ' '){
str += ' ';
}else {
//forloop to check the index of the character from array
for (int j = 1; j < letter.length; j++) {
if (letter[j] == c) {
index = j;
}
}
//shifting characters based on rot13
index = index % 26;
index = index + 13;
index = index % 26;
if (index == 0)
index = 26;
str += letter[index];
}
}
return str;
}//end encrypt
public String decrypt(String s){
String str = "";
//forloop to get the each character from the passing string
for(int i=0; i<s.length(); i++){
char c = Character.toUpperCase(s.charAt(i));
if(c == ' '){
str += ' ';
}else {
//forloop to check the index of the character from array
for (int j = 1; j < letter.length; j++) {
if (letter[j] == c) {
index = j;
}
}
//shifting characters based on rot13
index = index % 26;
index = index + 13;
index = index % 26;
if (index == 0)
index = 26;
str += letter[index];
}
}
return str;
}//end decrypt
}//结束类Rot13
我想解密使用 File 类制作的文件。
导入java.io.*;导入 java.util.Scanner;
公共类 FileExample 扩展 Rot13 {
public static void main(String [] args) {
try {
//create file object for input.txt
File in_file = new File("src/text.txt");
//create file object for output.txt
File out_file = new File("src/output.txt");
//read the input.txt file with Scanner
Scanner read = new Scanner(in_file);
//write the output.txt file with PrintWriter
PrintWriter w = new PrintWriter(out_file);
while(read.hasNextLine()){
w.write(read.nextLine());
}
while(read.hasNext()){
System.out.println(read.next());
}
//don't forget to close
w.close();
}
catch(Exception ex) {
ex.getStackTrace();
}
}
}
我不知道如何将带有加密消息的文本文件发送到解密类。谁能帮助我?
谢谢你。