doushou3814 2015-11-05 05:54 采纳率: 100%
浏览 76

使用频率分析来解码使用单个字符替换加密的文本文件

I have two files, one is the encryption of the original using frequency analysis, and just do a new string with the decoded string(some chars probably wont be correctilly changed, but its ok, ill change them manually later), but im completelly stuck on how to do this after counting the incidences on both files.

//gets string from both text files and converts in a array
        $reference = file_get_contents('reference_file.txt', true);
        $encrypted = file_get_contents('encrypted_file.txt', true);
        $refarray = str_split($reference, '1');
        $encarray = str_split($encrypted, '1');

//counts ocurrences in both strings and saves them as an array : "Character" ==> "Number of Ocurrences"
        $refarray1 = array_count_values(str_split(file_get_contents('reference_file.txt', true)));
        $refarray2 = array_count_values(str_split(file_get_contents('encrypted_file.txt', true)));

Now i dont know where to go from here, totally stuck

EDIT 1:

 foreach ($refarray1 as $key => $val) {
            print "$key = $val <br \>";
            $aux69 = $key;
            foreach ($refarray2 as $key2 => $val2) {
                if ($val == $val2) {
                   $encrypted = str_replace($key2, $key, $encrypted);
                }
            }
        }

I came up with this, but its still not working, it changes lots of letters in the encrypted string, but its unreadable, it changed most chars to only 'j' 'd' 'e', example of final string:

" jjdebdda bw d jdbejewedwbje zjee edzjdbbddda "

  • 写回答

1条回答 默认 最新

  • douqudi5282 2015-11-05 13:01
    关注

    In standard English text the most common character is a space. Some cryptographic plaintexts include spaces, others don't. You will need to try both. After space, the most common characters are ETA OIN SHR DLU in that order. Match those to the most frequent letters in the cyphertext for a crude first cut at a decryption.

    For progress beyond that, you might need something like a crossword dictionary, which can match patterns like "S.I.OT" to a word : spigot, and then extend the found 'p' and 'g' to other words. If you know the context of the cyphertext, then you can tweak your dictionary. Commercial text will contain words like "marketing", while military text will contain words like "battalion".

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器