尝试使用算法解密文本文件

时间:2014-11-19 10:57:06

标签: java

我有解密方法打开带有加密文本的测试文件,然后读取并解读我从输入文件读入的每行文本。文本文件名为mystery.txt。 我只能在输入单个字符时才能使用该方法,但是我无法在打开.txt文件并逐行解密的情况下使用它。我试过在算法之前调用我的readFile(),但是我在for(i = 0; i< text.length(); i ++)中得到了一个空指针异常。我以前从未做过类似的事情,也不知道如何去做。

该算法应读取每一行并使用crypt1& amp; crypt2变量。

使用我的readFile()和writeFile()方法,我需要我的cipherDecipherString()方法能够写一个打开文件的方法,一次读取它包含一行的每一行,解读每一行读取的行将结果文本输入并写入屏幕和输出文件。

解密方法:

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

我的readFile方法:

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

我的writeFile()方法:

public void writeFile()
{
    String myString;
    clrscr();
    System.out.println("Begin typing the contents to you wish to WRITE to file: ");
    myString = Genio.getString();
    System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    FileOutputStream outputStream = null;
    PrintWriter printWriter = null;
    try
    {
        outputStream = new FileOutputStream("writing.txt");
        printWriter = new PrintWriter(outputStream); 
        printWriter.write(myString);
        printWriter.println("\n");
        while (!myString.equals(""))
        {
            myString = Genio.getString();
            printWriter.print(myString + "\n");
        }

        System.out.println("File: 'writing.txt' has been saved with the contents above.\n\nYou can now open this file using the other options in the menu screen.");
        pressKey();
    }
    catch (IOException e)
    {
        System.out.println("Sorry, there has been a problem opening or writing to the file");
    }
    finally
    {
        if (printWriter != null)
        {
            // close the file
            printWriter.close();    
        }
    }
}

现在我认为我只能调用我的readFile()方法,然后进入解密代码,然后让它在文件中工作,但我根本无法使用它。

Genio是处理用户输入的类。我不想过度使用代码,但如果需要,可以发布添加代码。谢谢!

1 个答案:

答案 0 :(得分:1)

让我们将您的cipherDecipherString称为decode,然后输入另一个名为decode的方法,该方法需要两个文件路径。一个是源文件路径,另一个是目标。然后是代码的样子:

private static final String crypt1 = "cipherabdfgjk";
private static final String crypt2 = "lmnoqstuvwxyz";

private String decode (String line)
{
  StringBuilder result = new StringBuilder (line.length ());

  for (int i = 0; i < line.length (); ++i)
  {
    char ch = line.charAt (i);

    int index1 = crypt1.indexOf (ch);
    int index2 = crypt2.indexOf (ch);

    if (index1 != -1)
    {
      result.append (crypt2.charAt (index1));
    }
    else if (index2 != -1)
    {
      result.append (crypt1.charAt (index2));
    }
    else
    {
      result.append (ch);
    }
  }

  return result.toString ();
}

/* Takes file source, reads it line by line, decodes each line, then writes the
 * decoded lines to the file destination.
 */
public void decode (String fileSource, String fileDestination) throws IOException
{
  Files.write (Paths.get (fileDestination), 
               Files.lines (Paths.get (fileSource)).
               map (this::decode).
               collect (Collectors.toList ()));
}

这需要Java 8。 如果您的cipherDecipherString方法存在任何问题,请与我们联系。

这些是必需的导入:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;