【问题标题】:Caesar encrypted ASCII text partially decrypted凯撒加密的 ASCII 文本部分解密
【发布时间】:2017-01-22 10:09:43
【问题描述】:

我正在尝试通过暴力破解凯撒编码的 base 256 ASCII 文本。 有问题的文字是:

dy}uƒ0^u‡0b}q~K‹lvAlv‚}q~lv€‚Blvsxq‚ƒu„B0c‰}r|K‹lvBlvƒ‡yƒƒlv€‚Blvsxq‚ƒu„@0Q‚yq|K‹lvClv‚}q~lv€‚Blvsxq‚ƒu„@0\yru‚q„y~0cu‚yv‹l:lvq|„0dy}uƒ0^u‡0b}q~K‹lvDlvƒ‡yƒƒlv€‚Blvsxq‚ƒu„@0\yru‚q„y~0cq~ƒ‹l:lvq|„0Q‚yq|K‹lvElv‚}q~lv€‚@lvsxq‚ƒu„ABH0dy}uƒ0^u‡0b}q~K‹lvFlv~y|l

有了这个小的java程序:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class Run {
    private static FileWriter fr;
    static String b;
    private static BufferedReader br;

    public static void main(String[] args) throws IOException {

        // encrypted file
        File enc_f = new File("caesar.rtf.enc");
        //decrypted file
        File dec_f = new File("caesar.rtf.dec");

        // init variables
        String text_enc = new String();
        String text_dec = new String();

        // read file
        br = new BufferedReader(new FileReader(enc_f));
        for (String line; (line = br.readLine()) != null; text_enc += line);
        char[] stringToCharArray = text_enc.toCharArray();

        // parse file and convert string to char
        for (int shift = 0; shift < 257; shift++) {

            for (char output : stringToCharArray) {

                // convert ascii to int
                int ascii = (int) output;

                // shift 
                ascii = ascii + shift;
                ascii = ascii % 256;

                // convert back to ascii
                char chTemp = (char) ascii;
                text_dec += chTemp;

            }

            // visual representation
            text_dec += System.lineSeparator();
            text_dec += System.lineSeparator();
            text_dec += shift;
            text_dec += System.lineSeparator();
            System.out.println(shift);

            // write decrypted file
            fr = new FileWriter(dec_f);
            fr.write(text_dec);

        }
        fr.close();
    }
}

运行程序后,我得到了班次号239一个部分解密的文本(这只是整个文件的一部分,以保持执行时间很小):

Timeí Neí Roman;íí\f1\fíoman\fííí2\fchaííeí2 Símbol;íí\f2\fííiíí\fííí2\fchaííeí0 Aíial;íí\f3\fíoman\fííí2\fchaííeí0 Libeíaíion Seíifí\*\falí Timeí Neí Romaní;íí\f4\fííiíí\fííí2\fchaííeí0 Libeíaíion Saníí\*\falí Aíialí;íí\f5\fíoman\fííí0\fchaííeí128 Timeí Neí Roman;íí\f6\fnil\

可以看出,我可以阅读 Time new Roman,但也可以阅读 í,它不应该是,我不明白为什么,好像轮班不正确,而不是所有文本都应该是错误的只是其中的一部分。加密文本也可以正确解密。 如果您有任何想法,我们将不胜感激。

【问题讨论】:

  • 239 似乎不是正确的移位,因为 'd' 的 ascii 代码为 100,如果我们添加或减去 239,我们将超出 256 个字符的 ascii 表限制。
  • @AlexanderV。它不应该超出 ascii 限制,因为我输入了 'ascii = ascii % 256;'所以它将恢复到 256 限制

标签: java encryption cryptography ascii


【解决方案1】:

您犯了一个非常基本的错误:将二进制/字节与字符串混淆。

没有“256 ASCII”之类的东西,ASCII 是 7 位,即在 [0..127] 内编码,前 32 位和最后一个值是 控制字符

你说的是bytes,应该对它们执行操作。如果您在 Java 中对字节执行计算,它将自动在 0..255 范围内。尽管字节没有“提升”为整数,但您必须小心,不时使用(byte) 进行回滚。

因此,除了最终打印输出之外,您的操作都应该以字节为单位。使用ReaderWriter 可能会丢失数据,因为某些字符可能会被遗漏。只需直接使用流,然后在任何文本阅读器中查看输出。

您当然也可以使用输出介于某些字节值(有效字符编码)之间的事实来测试您的解决方案是否正确。


请注意,我们无法为您进行测试,因为您输入的“字符串”可能已经损坏。如果要在此处打印,请使用 base 64 或十六进制对其进行编码。

【讨论】:

  • 谢谢!我不知道我怎么会犯这样的错误。我完全按照你告诉我的做,使用'Files.readAllBytes(path_to_file);'读取字节,将它们转换为无符号一,然后转换为整数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-07
  • 1970-01-01
相关资源
最近更新 更多