Search code examples
javafileencryptionrot13

How can I decrypt a text file with my decryption class?


I have a text file that I need to be decrypted using this code:

//Rot13 encrypt and decrypt 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

}//end class Rot13

I want to decrypt a file that I made using the File class.

import java.io.*; import java.util.Scanner;

public class FileExample extends 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();
    }
}

}

I don't know how to send the text file with the encrypted message to the decryption class. Can anyone please assist me?

Thank you.


Solution

  • Your encrypt and decrypt methods expect a String as input.

    You can get the entire contents of the file in a single line, using the File.readAllBytes() method. FileExample extends Rot13, but the encrypt and decrypt methods are not static (static methods usually are a bad idea anyway).

    So, to call the decrypt method, you first need to create an instance of FileExamples, then call the decrypt method on the instance. In fact, it's better to move your logic from the main method into a non-static method in FileExample and just all that method from main.

    Finally, you write the decrypted String to your output file in a single line, using the Files.write() method

    See the code below:

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