Java中的解码器

时间:2015-12-27 01:29:42

标签: java

我有一个java文件,可以将文件编码为其他内容。我想知道是否有办法解码它。

我运行该文件3次,然后在.txt文件的开头添加斜杠并再次运行它。需要

"Hello World! This is the Computer!
Say Hi!" 

并给出

  

"   C   F   Ë   -1   -1   -1   '#: - 52'   '#: - 44'   '#: - 47'   F   G   d   -1   -1   -1"

代码如下:

package complexify;

public class Complexer {
public void Complex() {
    ReadFile r = new ReadFile();
    FileWriter f = new FileWriter();
    NumToChar n = new NumToChar();
    String oldContents = r.readFile("C:/Users/Work and PA/Desktop/Java Projects/Complexify/src/complexify/TextFile.txt");
    String newContents = "";
    String newContentsTemp = "";
    char[] contentsChar = oldContents.toCharArray();
    int letterPos = 0;
    boolean complex = false;
    if (contentsChar[0] == '/') {
        complex = false;
        //contentsChar[0] = '_';
    }
    else {
        complex = true;
    }
    if (complex == true) {
        for (int i = 0;i < contentsChar.length;i++) {
            char currentChar = contentsChar[i];
            letterPos = currentChar - 'a';
            newContents += letterPos + ",";
            newContentsTemp += letterPos + ",";
            if (newContentsTemp.length() == 100) {
                newContents += "\n";
                newContentsTemp = "";
            }
        }
        try {
            newContents = newContents.substring(0, newContents.length()-7);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
    if (complex == false) {
        for (int i = 0;i < contentsChar.length;i++) {
            char currentChar = contentsChar[i];
            if (currentChar == ',') {
                System.out.println(newContentsTemp);
                try {
                    newContents += (n.getCharForNumber(Integer.parseInt(newContentsTemp)));
                }
                catch (Exception e) {
                    System.out.println(e);
                }
            }
            else {
                newContentsTemp += currentChar;
                if (newContentsTemp.length() == 100) {
                    newContents += "\n";
                    newContentsTemp = "";
                }
            }
        }
    }
    System.out.println(newContents);
    f.write(newContents);
}

}

package complexify;

public class ComplexifyMain {

public static void main(String[] args) {
    Complexer c = new Complexer();
    c.Complex();
}

}

package complexify;

import java.io。*;

public class FileWriter {

public void write(String text) {
      String fileName = "C:/Users/Work and PA/Desktop/Java Projects/Complexify/src/complexify/TextFile.txt";

      try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"))) {
          writer.write(text);
      }
      catch(IOException ex) {
          System.out.println("Error writing to file '" + fileName + "'");
      }
}

}

package complexify;

public class NumToChar {
public String getCharForNumber(int i) {
    char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    char[] capAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    if (i > -1) {
        if (i > 25) {
            return "-1";
        }
        else {
            return Character.toString(alphabet[i]);
        }
    }
    //Capital Letters
    else if (i > -33 && i < -6) {
        i = i + 32;
        return Character.toString(capAlphabet[i]);
    }
    else {
        return "'#:" + i + "'";
    }
}

}

package complexify;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class ReadFile {
public String readFile(String file) {
    BufferedReader br = null;
    String contents = "";
    try {

        File f = new File(file);

        // if file doesn't exists, then create it
        if (!f.exists()) {
            f.createNewFile();
        }

        String sCurrentLine;

        br = new BufferedReader(new FileReader(file));

        while ((sCurrentLine = br.readLine()) != null) {
            //System.out.println(sCurrentLine);
            contents += sCurrentLine;
            contents += "\n";
        }

    } catch (IOException e) {
        e.printStackTrace();
    } 
    finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return contents;

}

}

抱歉格式化问题,谢谢!

1 个答案:

答案 0 :(得分:0)

Wheater一个代码与否可以解码取决于多种因素。作为重击规则,如果每个输入字母只能转换为另外一个字母,并且一个输出字母只能通过给定位置的一个输入字母产生,则代码可以被解码。

if (i > 25) {
   return "-1";
}

使得无法将25以上的任何数字与25以上的任何其他数字区分开来,并且您的输出代码包含-1,这似乎表示您无法再次解码这些字符。

您的程序代码看起来有点混乱。您应该考虑将部件放入较小的方法中,并为方法指定正确的名称。这将使您的代码更具可读性。目前你所做的一切都或多或少地用1-2种方法完成,你的代码似乎是多余的和错误的。

此外,您的代码似乎会剪切输入字符串的最后7个字母

newContents = newContents.substring(0, newContents.length()-7);

所以我的猜测是代码无法恢复到完整的输入消息。