| 1 | #!/usr/bin/php |
| 2 | <?php |
| 3 | //Frequency analysis 0.2 |
| 4 | //Harvie 2oo7 |
| 5 | /* |
| 6 | * Use this to crack monoalphabetic ciphers (vigenere,...) |
| 7 | * It needs good language file (ex: one for technical another for medical language, etc...) |
| 8 | * For more see this: http://en.wikipedia.org/wiki/Frequency_analysis_(cryptanalysis) |
| 9 | * |
| 10 | * Known bugs: this can assign one letter to more crypted letters |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | function freq_analyze($infile, $cereg = '[a-z ]') { //Make frequency fingerprint |
| 15 | $debug = 100; |
| 16 | $total = 0; |
| 17 | $in = fopen($infile, 'r'); |
| 18 | while(!feof($in)) { |
| 19 | $c = strtolower(fgetc($in)); |
| 20 | if(eregi($cereg, $c)) { |
| 21 | if(!isset($data[$c])) $data[$c] = 0; |
| 22 | $data[$c]++; |
| 23 | $total++; |
| 24 | } |
| 25 | } |
| 26 | fclose($in); |
| 27 | |
| 28 | //Compute percents |
| 29 | foreach($data as $c => $n) { |
| 30 | $data[$c] = ($n/$total)*100; |
| 31 | $debug -= $data[$c]; |
| 32 | } |
| 33 | |
| 34 | //echo("Debug: $debug\n"); //Debug |
| 35 | return($data); |
| 36 | } |
| 37 | |
| 38 | function freq_compare($cipher, $language) { //Do analysis (from two fingerprints) |
| 39 | foreach($cipher as $c => $n) { |
| 40 | $min = 1024; |
| 41 | foreach($language as $dc => $dn) { |
| 42 | $nmin = abs($n-$dn); |
| 43 | //echo $nmin."\n"; //Debug |
| 44 | if($nmin < $min) { |
| 45 | $min = $nmin; |
| 46 | $decrypted[$c] = $dc; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return($decrypted); |
| 52 | } |
| 53 | |
| 54 | function freq_errors($decrypted) { //Test success (run with uncrypted cipher) |
| 55 | $errors = 0; |
| 56 | $total = 0; |
| 57 | foreach ($decrypted as $a => $b) { |
| 58 | if($a != $b) $errors++; |
| 59 | $total++; |
| 60 | } |
| 61 | $hits = $total - $errors; |
| 62 | $perc = round(($hits/$total)*100); |
| 63 | echo("$hits hits and $errors errors / $total total - that means, you are $perc% successfull\n"); |
| 64 | return $errors; |
| 65 | } |
| 66 | |
| 67 | ///CODE////////////////////////////////////////////////////////////////////////// |
| 68 | $czech = freq_analyze('czech.txt'); |
| 69 | //print_r($czech); //Debug |
| 70 | $cipher = freq_analyze('cipher.txt'); |
| 71 | //print_r($cipher); //Debug |
| 72 | |
| 73 | $decrypted = freq_compare($cipher, $czech); |
| 74 | freq_errors($decrypted); |
| 75 | print_r($decrypted); |