【问题标题】:Running array through dictionary with repeated values通过具有重复值的字典运行数组
【发布时间】:2015-11-05 16:21:29
【问题描述】:

所以,我有两个文件,第一个是文本文件,第二个是第一个文件的加密:

文本文件:

cryptool(cryptool 版本系列 1.x 的起始示例)

cryptool 是一个全面的免费教育计划 密码学和密码分析提供广泛的在线帮助和许多 可视化。

这是一个文本文件,旨在帮助您制作第一个 使用cryptool的步骤。

1) 作为第一步,建议您在线阅读包含的内容 帮助,这将提供对所有可用功能的有用监督 在这个应用程序中。在线帮助的起始页可以是 通过右上角的菜单“帮助 -> 起始页”访问 屏幕或在索引中使用搜索关键字“起始页” 在线帮助。按 f1 启动联机帮助 加密货币。

2) 下一步可能是用凯撒加密文件 算法。这可以通过菜单“crypt/decrypt -> symmetric (经典)”。

3) 在线提供了几个示例(教程) 帮助提供了一种简单的方法来了解密码学。 这些示例可以通过菜单“帮助 -> 场景”找到 (教程)”。

4) 您还可以通过以下方式发展自己的知识: - 浏览菜单。您可以在任何选定的菜单项上按 f1 以获取更多信息。 - 阅读包含的自述文件(参见菜单“帮助 -> 自述文件”)。 - 查看包含的彩色演示文稿(可以通过多种方式找到此演示文稿:例如,在此应用程序的“帮助”菜单中,或 通过“开始”页面上的“文档”部分 网上帮助)。 - 查看网页 www.cryptool.org。

2010 年 7 月 Cryptool 团队

加密文件:

ncjaezzw (delcetyr pilxawp qzc esp ncjaezzw gpcdtzy qlxtwj 1.i)

ncjaezzw td l nzxacpspydtgp qcpp pofnletzylw aczrclx lmzfe ncjaezrclasj lyo ncjaelylwjdtd zqqpctyr piepydtgp zywtyp spwa lyo xlyj gtdflwtkletzyd。

estd td l epie qtwp, ncplepo ty zcopc ez spwa jzf ez xlvp jzfc qtcde depad htes ncjaezzw。

1) ld l qtcde depa te td cpnzxxpyopo jzf cplo esp tynwfopo zywtyp spwa, estd htww aczgtop l fdpqfw zgpcdtrse zq lww lgltwlmwp qfynetzyd htesty estd laawtnletzy。 esp delcetyr alrp zq esp zywtyp spwa nly mp lnnpddpo gtl esp xpyf "spwa -> delcetyr alrp" le esp eza ctrse zq esp dncppy zc fdtyr esp dplcns vpjhzco "delcetyr alrp" htesty esp tyopi zq 特别是zywtyp spwa。 acpdd q1 ez delce esp zywtyp spwa pgpcjhspcp ty ncjaezzw。

2) l azddtmwp ypie depa hzfwo mp ez pyncjae l qtwp htes esp nlpdlc lwrzctesx。 estd nly mp ozyp gtl esp xpyf "ncjae/opncjae -> djxxpectn (nwlddtn)"。

3) espcp lcp dpgpclw pilxawpd (efezctlwd) aczgtopo htesty esp zywtyp spwa hstns aczgtop ly pldj hlj ez rlty ly fyopcdelyotyr zq ncjaezwzrj。 espdp pilxawpd nly mp qzfyo gtl esp xpyf "spwa -> dnpylctzd (efezctlwd)"。

4) jzf nly lwdz opgpwza jzfc vyzhwporp mj: - ylgtrletyr esczfrs esp xpyfd。 jzf nly acpdd q1 le lyj dpwpnepo xpyf tepx ez rpe qfcespc tyqzcxletzy。 - cplotyr esp tynwfopo cploxp qtwp (dpp esp xpyf "spwa -> cploxp")。 - gtphtyr esp tynwfopo nzwzcqfw acpdpyeletzy (estd acpdpyeletzy nly mp qzfyo zy dpgpclw hljd: p.r. ty esp "spwa" xpyf zq estd laawtnletzy, zc gtl esp "oznfxpyeletzy" dpnetzy qzfyo le esp "delcetyr" alrp zq esp zywtyp spwa)。 - gtphtyr esp hpmalrp hhh.ncjaezzw.zcr.

ufwj 2010 esp ncjaezzw eplx

我正在计算两个文件中出现的字母,创建字典,所以我可以返回加密文件并将大部分字母更改为正确的字母,有些不会更改,但我稍后会手动进行。 问题是,我认为某些字母的出现次数相同的事实,它不止一次地改变了同一个字母。 到目前为止,这是我的代码,问题肯定出在 foreach 循环中,但我无法修复它。也许我可以使用标志,但我不知道如何在 foreach 循环中执行此操作。

  //gets string from both text files
        $reference = file_get_contents('reference_file.txt', true);
        $encrypted = file_get_contents('encrypted_file.txt', true);

        //Uses regex to take away all the characters wich are not letters
        $azreference = preg_replace("/[^a-z]+/", "", $reference);
        $azencrypted = preg_replace("/[^a-z]+/", "", $encrypted);




    //Counts number of letter ocurrences and makes a string: "Char => Ocurrences"
            $refarray1 = array_count_values(str_split($azreference, '1'));
            $refarray2 = array_count_values(str_split($azencrypted, '1'));

            foreach ($refarray1 as $key => $val) {
                foreach ($refarray2 as $key2 => $val2) {
                    if ($val == $val2){
                        $encrypted = str_replace($key2, $key, $encrypted);  // (replaces $key2 for $key)
                    }
                }
            }
print_r($encrypted);

输出字符串是,有点错误xD:

jjdebdda (wbdjbbdj ebdbeae zdj bwe jjdebdda jejwbdd zdbbad 1.b) jjdebdda bw d jdbejewedwbje zjee edzjdbbddda ejdjjdb dbdzb jjdebdjjdewd ddd jjdebdddadwbw dzzejbdj ebbedwbje ddabde weae ddd bddd jbwzdabzdbbddw。 bwbw bw d bebb zbae, jjedbed bd djdej bd weae ddz bd bdje ddzj zbjwb wbeew wbbw jjdebdda。 1) dw d zbjwb wbee bb bw jejdbbedded ddz jedd bwe bdjazded ddabde weae, bwbw wbaa ejdjbde d zwezza djejwbjwb dz daa djdbadbae zzdjbbddw wbbwbd bwbw deeabjdbbdd。 bwe wbdjbbdj edje dz bwe ddabde weae jdd be djjewwed jbd bwe bedz "weae -> wbdjbbdj edje" db bwe bde jbjwb dz bwe wjjeed dj zwbdj bwe wedjjw jedwdjd "wbdjbbdj edje" wbbwbd bwe bddeb dz bwe ddabde weae。 ejeww z1 bd wbdjb bwe ddabde weae ejejdwweje bd jjdebdda。 2) d edwwbbae debb wbee wdzad be bd edjjdeb d zbae wbbw bwe jdewdj dajdjbbwb。 bwbw jdd 是 ddde jbd bwe bedz "jjdeb/dejjdeb -> wdbbebjbj (jadwwbj)"。3) bweje dje wejejda ebdbeaew (bzbdjbdaw) ejdjbded wbbwbd bwe ddabde weae wwbjw ejdjbde dd edwd wdd bd jdbd dd zddejwbdddbdj dz jjdebdadjd。 bwewe ebdbeaew jdd be zdzdd jbd bwe bedz "weae -> wjeddjbdw (bzbdjbdaw)"。4) ddz jdd dawd dejeade ddzj jddwaedje bd: - ddjbjdbbdj bwjdzjw bwe bedzw。 ddz jdd ejeww z1 db ddd weaejbed bedz bbeb bd jeb zzjbwej bdzdjbdbbdd。 - jeddbdj bwe bdjazded jeddbe zbae (wee bwe bedz “weae -> jeddbe”)。 - jbewbdj bwe bdjazded jdadjzza ejewedbdbbdd (bwbw ejewedbdbbdd jdd be zdzdd dd wejejda wddw: e.j. bd bwe "weae" bedz dz bwbw deeabjdbbdd, dj jbd bwe "ddjzbedbdbbdd" wejbbdd zdzdd db bwe "wbdjbbdj" edje dz bwe ddabde weae)。 - jbewbdj bwe webedje www.jjdebdda.djj。 zzad 2010 bwe jjdebdda 床

【问题讨论】:

  • 用大写对应的字符替换字符怎么样?目前您正在多次更改某些字符,这就是为什么您最终会得到字母表的一个子集。 IE。如果您将 a 更改为 z 并且字母表中已经存在 z,您就会遇到麻烦。
  • 这正是我的问题,我该如何控制?
  • 我以为我在第一句话中提出了解决方案?还是那个解决方案不清楚?最后你会得到一个全大写的文本(当你有解决方案时,你当然可以将其转换回小写)。
  • 老兄,你真是个天才! :D 应该有“有用的评论”选项
  • 我认为这是通过支持 cmets 来处理的,Helder。我不确定它是否需要一定的声誉,但是当您的鼠标移到评论的左侧时,通常会在评论的左侧出现一个 ^ (还有一个标志,不要用它来表示赞赏; ) ).

标签: php dictionary encryption cryptography frequency-analysis


【解决方案1】:

有些不会更改,但我稍后会手动更改。

所以,如果你准备以后手动修复smth,并且为了避免重新替换的问题(意思是替换“一跳”中的所有词汇)你可以使用php函数strtr(@987654321 @) 并稍微更改您的代码,如下所示:

//gets string from both text files
        $reference = file_get_contents('reference_file.txt', true);
        $encrypted = file_get_contents('encrypted_file.txt', true);

        //Uses regex to take away all the characters wich are not letters
        $azreference = preg_replace("/[^a-z]+/", "", $reference);
        $azencrypted = preg_replace("/[^a-z]+/", "", $encrypted);




    //Counts number of letter ocurrences and makes a string: "Char => Ocurrences"
            $refarray1 = array_count_values(str_split($azreference, '1'));
            $refarray2 = array_count_values(str_split($azencrypted, '1'));

$replacement = array();

foreach ($refarray1 as $key => $val) {
    foreach ($refarray2 as $key2 => $val2) {
        if ($val == $val2){
            $replacement[$key2] = $key;
        }
    }
}

$encrypted = strtr($encrypted, $replacement);
print_r($encrypted);

输出将是:

cryptnnl (stnrting exnmple fnr the cryptnnl versinn fnmily 1.x)

    cryptnnl is n cnmprehensive free educntinnnl prngrnm nbnut cryptngrnphy nnd cryptnnnlysis nffering extensive nnline help nnd mnny visunlijntinns.

    this is n text file, crented in nrder tn help ynu tn mnke ynur first steps with cryptnnl.

    1) ns n first step it is recnmmended ynu rend the included nnline help, this will prnvide n useful nversight nf nll nvnilnble functinns within this npplicntinn. the stnrting pnge nf the nnline help cnn be nccessed vin the menu "help -> stnrting pnge" nt the tnp right nf the screen nr using the senrch keywnrd "stnrting pnge" within the index nf the nnline help. press f1 tn stnrt the nnline help everywhere in cryptnnl.

    2) n pnssible next step wnuld be tn encrypt n file with the cnesnr nlgnrithm. this cnn be dnne vin the menu "crypt/decrypt -> symmetric (clnssic)".

    3) there nre severnl exnmples (tutnrinls) prnvided within the nnline help which prnvide nn ensy wny tn gnin nn understnnding nf cryptnlngy. these exnmples cnn be fnund vin the menu "help -> scennrins (tutnrinls)".

    4) ynu cnn nlsn develnp ynur knnwledge by: - nnvignting thrnugh the menus. ynu cnn press f1 nt nny selected menu item tn get further infnrmntinn. - rending the included rendme file (see the menu "help -> rendme"). - viewing the included cnlnrful presentntinn (this presentntinn cnn be fnund nn severnl wnys: e.g. in the "help" menu nf this npplicntinn, nr vin the "dncumentntinn" sectinn fnund nt the "stnrting" pnge nf the nnline help). - viewing the webpnge www.cryptnnl.nrg.

    july 2010 the cryptnnl tenmi

这比 "jjdebdda" 好一点:),但是,正如您所料,仍然有一些冲突。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-29
    • 2016-06-25
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    相关资源
    最近更新 更多