如何在java中的caesar加密中包含文件处理?

如何在java中的caesar加密中包含文件处理?,java,encryption,file-handling,Java,Encryption,File Handling,这是我简单的文本加密代码,但我的老师说我必须编写一个文件处理程序。但我不知道如何编程,如何加密和解密导入的文件。请帮帮我 package caesarchiffre; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class Caeser {

这是我简单的文本加密代码,但我的老师说我必须编写一个文件处理程序。但我不知道如何编程,如何加密和解密导入的文件。请帮帮我

package caesarchiffre;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Caeser {

    public static void main(String[] args) throws IOException {

    int key = 6; //Einwegschlüssel für Ver- und Entschlüsselung

    String text = "Hallo!";
    System.out.println(text);

    char[] chars = text.toCharArray(); //Character-Array mit String Methode zum umwandeln in Char

    //Verschlüsselung
    for(char c :chars) {
        c += key;
        System.out.print(c);
    }
    
    //Entschlüsselung
    //for(char c :chars) {
    //    c -= key;
    //    System.out.print(c); }

   }
}

下面是一些关于文件处理的概念,简要说明。请阅读关于它们的现有资源

扫描文件:使用Java
Scanner

以下是您如何实现它:

import java.util.Scanner;
import java.util.File;
File file = new File(pathname)
import java.util.FileWriter;

FileWriter fileWriter = new FileWriter(pathname);
//pathname of file to write to

fileWriter.write("Hello World! This is the first Caesar Cipher of Elias M.");
路径名只是要扫描的文件路径名的占位符。有关如何获取文件路径名的照片,请参阅

Scanner scanner = new Scanner(file) 
在这一点上,你有你的扫描仪

写入文件:使用Java
FileWriter

以下是您如何实现它:

import java.util.Scanner;
import java.util.File;
File file = new File(pathname)
import java.util.FileWriter;

FileWriter fileWriter = new FileWriter(pathname);
//pathname of file to write to

fileWriter.write("Hello World! This is the first Caesar Cipher of Elias M.");
您可以使用
扫描仪
文件编写器
执行任何操作。然而,在您的例子中,您正在扫描一个文件,可能其中包含要编码的文本。因此,您可以这样做:

char[] alphabet = {'a', 'b','c'};//... and so on
char[] shiftedAlphabet = {'r','s','t'};//...and so on
这是一个将每个字母上移16位的示例:

/*
a--->r
b--->s
c--->t
*/
打开以扫描文件:

while (scanner.hasNext()){
    /*
    while scanner has a next String. That is, your text is not over:
    */
    String next = scanner.next(); //the next string in the file
假设您有一个仅包含单词
first caesar cipher
的文件

扫描仪首先查看“first”,因为它是第一个单词。正如您将在下面看到的,您可以让它查看“first”中的每个
char
: f、 i,r,s,t。然后转到“凯撒”,看c,a,e,s,a,r。最后,它转到“密码”并查看c、i、p、h、e、r

请注意,默认情况下,
scanner.next()
逐字扫描文件,就好像按照“first”、“caesar”、“cipher”来查看文件一样。但是,正如您将在下面看到的,我们可以使用一个简单的
for
循环来逐字进行操作:

    StringBuilder code = new StringBuilder(); //empty for now
    for (int i = 0; i < next.length; i++){

        char ch = next.charAt(i); 
        /*
        explanation of charAt(index):
        Let us say that your word is "caesar". 
        the loop goes to charAt(0), which is 'c'
        then it goes to charAt(1), which is 'a', and so on. 
        */
        for (int j = 0; j < alphabet.length; j++){
            char letter = alphabet[j];
            char shiftedEquivalent = alphabetShifted[j]
            if (ch == letter){
                code.append(shiftedEquivalent);
            }
        }
    }
    fileWriter.write(code); //write your first word into the file.
    //This loops over and over until the text file has no more text.
}
StringBuilder code=new StringBuilder()//暂时空着
for(int i=0;i
请仔细查看上面的for循环。使用我们的理论文件:

-扫描仪转到“凯撒”

-
循环选择“c”

-另一个
for
循环遍历字母表,找出哪个字母等于c。

这听起来有些多余,但计算机并不知道c是字母表中的第三个字母。因此,在第三次迭代中,如果找到“c”并说,“啊哈!
char ch
是“c”,而这个字母是“c”,太棒了!”它会注意到c是字母表中的第三个字母

-然后它转到移位字母表中的第三个字母,在我们的移位+16示例中为't',并将
apppend()
(意思是将其添加到StringBuilder
code

如果更改移位字母表,则更改移位,从而更改密码。 为简单起见,这只允许创建一种类型的Caesar密码,除非手动编辑数组。如果您想要一个关于如何动态创建和破解Caesar密码的个人示例,很快就会上传


希望这篇文章能对你有所帮助-也就是“希望这有帮助”-但这是禁忌。

看看这篇文章,我还是不明白对不起。我是JavaTry的初学者,下面的答案只是一个复制粘贴。它有效;)回答得很好。此外,如果对扫描仪的危险感兴趣,请联系: