简体   繁体   中英

Decipher encrypted text file

I have deciphering method that should open a test file with encrypted text, then read and decipher each line of text that I read in from the input file. The text file is called mystery.txt. I can get the method to work when only inputting single characters but I can't get it to work where I open the .txt file and decipher line by line.

Dechiphering method:

public static String cipherDecipherString(String text)

{
 // These are global. Put here for space saving
 private static final String crypt1 = "cipherabdfgjk";
 private static final String crypt2 = "lmnoqstuvwxyz";

    // declare variables
    int i, j;
    boolean found = false;
    String temp="" ; // empty String to hold converted text
    readFile();
    for (i = 0; i < text.length(); i++) // look at every chracter in text
    {
        found = false;
        if ((j = crypt1.indexOf(text.charAt(i))) > -1) // is char in crypt1?
        {           
            found = true; // yes!
            temp = temp + crypt2.charAt(j); // add the cipher character to temp
        }
        else if ((j = crypt2.indexOf(text.charAt(i))) > -1) // and so on
        {
            found = true;
            temp = temp + crypt1.charAt(j);
        }
        if (! found) // to deal with cases where char is NOT in crypt2 or 2
        {
            temp = temp + text.charAt(i); // just copy across the character
        }
    }
    return temp;
}

My readFile method:

public static void readFile()
{
    FileReader fileReader = null;
    BufferedReader bufferedReader = null;
    String InputFileName;
    String nextLine;
    clrscr();
    System.out.println("Please enter the name of the file that is to be READ (e.g. aFile.txt: ");
    InputFileName = Genio.getString();
    try
    {
        fileReader = new FileReader(InputFileName);
        bufferedReader = new BufferedReader(fileReader); 
        nextLine = bufferedReader.readLine();
        while (nextLine != null)
        {
            System.out.println(nextLine);
            nextLine = bufferedReader.readLine();
        }
    }
    catch (IOException e)
    {
        System.out.println("Sorry, there has been a problem opening or reading from the file");
    }
    finally
    {
        if (bufferedReader != null)
        {
            try
            {
                bufferedReader.close();    
            }
            catch (IOException e)
            {
                System.out.println("An error occurred when attempting to close the file");
            }
        }  
    }
}

Now I thought that I would just be able to call my readFile() method then go into the decipher code and it let work it's way through the file but I cannot get it to work at all.

In readFile() you aren't doing anything with the lines you read, you aren't calling cipherDecipherString() anywhere.

Edit: You can add all the lines from the file to an array and return the array from the fuction. Then iterate through that array and decipher line by line

Change the readFile() return type to ArrayList;

ArrayList<String> textLines = new ArrayList<>();
while(nextLine != null) {
    textLines.add(nextLine);
    nextLine = bufferedReader.readLine();
}

return textLines;

Then in cipherDecipherString() call readFile().

ArrayList<String> textLines = readFile();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM