3 //Frequency analysis 0.2
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)
10 * Known bugs: this can assign one letter to more crypted letters
14 function freq_analyze($infile, $cereg = '[a-z ]') { //Make frequency fingerprint
17 $in = fopen($infile, 'r');
19 $c = strtolower(fgetc($in));
20 if(eregi($cereg, $c)) {
21 if(!isset($data[$c])) $data[$c] = 0;
29 foreach($data as $c => $n) {
30 $data[$c] = ($n/$total)*100;
34 //echo("Debug: $debug\n"); //Debug
38 function freq_compare($cipher, $language) { //Do analysis (from two fingerprints)
39 foreach($cipher as $c => $n) {
41 foreach($language as $dc => $dn) {
43 //echo $nmin."\n"; //Debug
54 function freq_errors($decrypted) { //Test success (run with uncrypted cipher)
57 foreach ($decrypted as $a => $b) {
58 if($a != $b) $errors++
;
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");
67 ///CODE//////////////////////////////////////////////////////////////////////////
68 $czech = freq_analyze('czech.txt');
69 //print_r($czech); //Debug
70 $cipher = freq_analyze('cipher.txt');
71 //print_r($cipher); //Debug
73 $decrypted = freq_compare($cipher, $czech);
74 freq_errors($decrypted);
This page took 1.634016 seconds and 4 git commands to generate.