0

This is my code for an easy encryption of text, but my teacher says that I have to program a file-handling. But I don't know how to program this and how I can encrypt and decrypt the imported file. Please help me!

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); }

   }
}
EliasM
  • 19
  • 2

1 Answers1

1

Here are some concepts around file handling, briefly explained. Please read existing resources about them, too!

Scanning Files: Use Java Scanner

Here is how you implement it:

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

pathname is just a placeholder for the pathname of the file you want to scan. See this post for photos of how to get the pathname of a file.

Scanner scanner = new Scanner(file) 

At this point, you have your scanner.

Writing to Files: Use Java FileWriter

Here is how you implement it:

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.");

You can do whatever you want with Scanner and FileWriter. However, in your case, you are scanning a file, presumably with text to encode in it. Therefore, you would do something like this:

char[] alphabet = {'a', 'b','c'};//... and so on
char[] shiftedAlphabet = {'r','s','t'};//...and so on

This is an example where you would shift each letter up 16 places:

/*
a--->r
b--->s
c--->t
*/

On to scanning the file:

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

Let us say that you have a file which contains only the words first caesar cipher.

The scanner first looks at "first" because it is the first word. As you will see below, you can then make it look at each char in "first": f, i, r, s, t. It then goes to "caesar" and looks at c,a,e,s,a,r. Finally, it goes to "cipher" and looks at c,i,p,h,e,r.

Note that scanner.next() by default scans word by word, as if looking at your file in terms of "first", "caesar", "cipher". However, as you will see below, we can make it go letter by letter using a simple for loop:

    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.
}

Please take a careful look at the for loop above. Using our theoretical file:

-Scanner goes to "caesar"

-the for loop picks "c"

-another for loop runs through the alphabet to find which alphabet letter equals c.

This sounds redundant, but the computer does not really know c is the third letter of the alphabet. So on the third iteration, if finds 'c' and says, "aha! char ch is 'c' and this letter is 'c', great!" It then notes that c is the 3rd letter of the alphabet.

-Then it goes to the third letter in shiftedAlphabet, in our example of shift +16, 't', and will apppend() (meaning add) it to the StringBuilder code.

If you change your shiftedAlphabet, you change the shift, and thus change the cipher. For simplicity, this only allows the creation of one type of Caesar Cipher unless you manually edit the array. If you would like a personal example on how to create and crack Caesar Ciphers dynamically, that will be uploaded shortly.

May this post help you - a.k.a. "hope this helps" - but that is taboo.

Cole Henrich
  • 155
  • 3
  • 17
  • great answer. also related, if interested on the dangers of the scanner : https://stackoverflow.com/a/65515359/2148953 – aran Feb 05 '21 at 22:28