如何使用我的解密 class 解密文本文件?
How can I decrypt a text file with my decryption class?
我有一个文本文件需要使用此代码解密:
//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
}//结束class Rot13
我想解密我使用文件 class 制作的文件。
导入java.io.*;
导入 java.util.Scanner;
public class 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();
}
}
}
我不知道如何将带有加密信息的文本文件发送给解密class。任何人都可以帮助我吗?
谢谢。
您的加密和解密方法需要一个字符串作为输入。
您可以使用 File.readAllBytes()
方法在一行中获取文件的全部内容。 FileExample
扩展了 Rot13
,但加密和解密方法不是静态的(无论如何,静态方法通常不是一个好主意)。
因此,要调用解密方法,首先需要创建FileExamples
的实例,然后在该实例上调用解密方法。事实上,最好将您的逻辑从 main 方法移动到 FileExample
中的非静态方法,并且只移动 main 中的所有方法。
最后,使用 Files.write()
方法
将解密的 String
写入单行输出文件
查看下面的代码:
public class FileExample extends Rot13 {
public void decryptFile(String inputFilePath, String outputFilePath){
try {
//Get the encrypted file contents as a String
String encryptedContents = new String(Files.readAllBytes(Paths.get(inputFilePath)));
//Call the decrypt method - inherited from Rot13
String decryptedContents = decrypt(encryptedContents);
//Write the decrypted content to the output file
Files.write(Paths.get(outputFilePath), decryptedContents.getBytes());
}
catch(Exception ex) {
ex.getStackTrace();
}
}
public static void main(String [] args) {
FileExample example = new FileExample();
example.decryptFile("src/text.txt", "src/output.txt");
}
}
我有一个文本文件需要使用此代码解密:
//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
}//结束class Rot13
我想解密我使用文件 class 制作的文件。
导入java.io.*; 导入 java.util.Scanner;
public class 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();
}
}
}
我不知道如何将带有加密信息的文本文件发送给解密class。任何人都可以帮助我吗?
谢谢。
您的加密和解密方法需要一个字符串作为输入。
您可以使用 File.readAllBytes()
方法在一行中获取文件的全部内容。 FileExample
扩展了 Rot13
,但加密和解密方法不是静态的(无论如何,静态方法通常不是一个好主意)。
因此,要调用解密方法,首先需要创建FileExamples
的实例,然后在该实例上调用解密方法。事实上,最好将您的逻辑从 main 方法移动到 FileExample
中的非静态方法,并且只移动 main 中的所有方法。
最后,使用 Files.write()
方法
String
写入单行输出文件
查看下面的代码:
public class FileExample extends Rot13 {
public void decryptFile(String inputFilePath, String outputFilePath){
try {
//Get the encrypted file contents as a String
String encryptedContents = new String(Files.readAllBytes(Paths.get(inputFilePath)));
//Call the decrypt method - inherited from Rot13
String decryptedContents = decrypt(encryptedContents);
//Write the decrypted content to the output file
Files.write(Paths.get(outputFilePath), decryptedContents.getBytes());
}
catch(Exception ex) {
ex.getStackTrace();
}
}
public static void main(String [] args) {
FileExample example = new FileExample();
example.decryptFile("src/text.txt", "src/output.txt");
}
}