0

So, i have two files, first is a text file, and the second is a encryption of the first file:

textfile:

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

encrypted file:

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

Im counting letter ocurrences in both files, creating a dictionary, so i can go back into the encrypted file and change most of the letters to the right ones, some wont be changed but i'll do it manually later. Problem is, i think the fact that some letters have the same number of ocurrences, its changing the same letter more than one time. Heres my code so far, the problem is surelly in the foreach loops but im not managing to fix it. Maybe i can use flags but i have no idea how to do this in a foreach cycle.

  //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);

The output string is, wich is kinda wrong 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

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Helder Ferreira
  • 285
  • 2
  • 3
  • 18
  • 2
    What about replacing the characters with their uppercase counterparts? Currently you are changing some characters multiple times, which is why you end up with a subset of the alphabet. I.e. if you change a to z and there are already z's in the alphabet you run into trouble. – Maarten Bodewes Nov 05 '15 at 16:44
  • thats exactilly my problem, how can i control this? – Helder Ferreira Nov 05 '15 at 16:48
  • I thought I brought up a solution in the first sentence? Or is that solution not clear? In the end you would have an all uppercase text (which you could of course convert back to lowercase when you have the solution). – Maarten Bodewes Nov 05 '15 at 16:50
  • dude, you're a genious! :D there should be the "Usefull comment" option for that – Helder Ferreira Nov 05 '15 at 17:06
  • I think that is handled by upvoting comments, Helder. I'm not sure that it requires a certain amount of reputation, but usually there appears a ^ on the left hand side of the comment when your mouse moves over it (as well as a flag, don't use that to show appreciation ;) ). – Maarten Bodewes Nov 05 '15 at 17:10

1 Answers1

2

some wont be changed but i'll do it manually later.

So, if you are ready to fix smth later manually, and in order to avoid the problem of re-replacing (meaning replace all the vocabulary in "one hop") you can use the php function strtr (http://php.net/manual/en/function.strtr.php) and change your code just a bit, like the following:

//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);

The output will be:

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

which is a bit better than "jjdebdda" :) , but, as you expected, still has some collisions.

user2655603
  • 410
  • 3
  • 11