运行 通过具有重复值的字典排列
Running array through dictionary with repeated values
所以,我有两个文件,第一个是文本文件,第二个是第一个文件的加密文件:
文本文件:
cryptool (starting example for the cryptool version family 1.x)
cryptool is a comprehensive free educational program about
cryptography and cryptanalysis offering extensive online help and many
visualizations.
this is a text file, created in order to help you to make your first
steps with cryptool.
1) as a first step it is recommended you read the included online
help, this will provide a useful oversight of all available functions
within this application. the starting page of the online help can be
accessed via the menu "help -> starting page" at the top right of the
screen or using the search keyword "starting page" within the index of
the online help. press f1 to start the online help everywhere in
cryptool.
2) a possible next step would be to encrypt a file with the caesar
algorithm. this can be done via the menu "crypt/decrypt -> symmetric
(classic)".
3) there are several examples (tutorials) provided within the online
help which provide an easy way to gain an understanding of cryptology.
these examples can be found via the menu "help -> scenarios
(tutorials)".
4) you can also develop your knowledge by:
- navigating through the menus. you can press f1 at any selected menu item to get further information.
- reading the included readme file (see the menu "help -> readme").
- viewing the included colorful presentation (this presentation can be found on several ways: e.g. in the "help" menu of this application, or
via the "documentation" section found at the "starting" page of the
online help).
- viewing the webpage www.cryptool.org.
july 2010 the cryptool team
加密文件:
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
esp 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 be 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 bedb
some wont be changed but i'll do it manually later.
因此,如果您准备稍后手动修复 smth,并且为了避免重新替换(意味着替换 "one hop" 中的所有词汇)的问题,您可以使用 php函数 strtr
(http://php.net/manual/en/function.strtr.php) 并稍微更改您的代码,如下所示:
//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" 好一点:),但是,如您所料,仍然有一些冲突。
所以,我有两个文件,第一个是文本文件,第二个是第一个文件的加密文件:
文本文件:
cryptool (starting example for the cryptool version family 1.x)
cryptool is a comprehensive free educational program about cryptography and cryptanalysis offering extensive online help and many visualizations.
this is a text file, created in order to help you to make your first steps with cryptool.
1) as a first step it is recommended you read the included online help, this will provide a useful oversight of all available functions within this application. the starting page of the online help can be accessed via the menu "help -> starting page" at the top right of the screen or using the search keyword "starting page" within the index of the online help. press f1 to start the online help everywhere in cryptool.
2) a possible next step would be to encrypt a file with the caesar algorithm. this can be done via the menu "crypt/decrypt -> symmetric (classic)".
3) there are several examples (tutorials) provided within the online help which provide an easy way to gain an understanding of cryptology. these examples can be found via the menu "help -> scenarios (tutorials)".
4) you can also develop your knowledge by: - navigating through the menus. you can press f1 at any selected menu item to get further information. - reading the included readme file (see the menu "help -> readme"). - viewing the included colorful presentation (this presentation can be found on several ways: e.g. in the "help" menu of this application, or via the "documentation" section found at the "starting" page of the online help). - viewing the webpage www.cryptool.org.
july 2010 the cryptool team
加密文件:
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 esp 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 be 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 bedb
some wont be changed but i'll do it manually later.
因此,如果您准备稍后手动修复 smth,并且为了避免重新替换(意味着替换 "one hop" 中的所有词汇)的问题,您可以使用 php函数 strtr
(http://php.net/manual/en/function.strtr.php) 并稍微更改您的代码,如下所示:
//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" 好一点:),但是,如您所料,仍然有一些冲突。