#!/usr/local/bin/perl5 -w ################################################################ ## ## Paul Garrett, garrett@math.umn.edu, 15 Sept 1997 ## ## Script to compute Friedman's "Index of Coincidence" of a string ## and a forward-shift of that string, in order to guess the ## period of a periodic substitution cipher ## ## Uses reference file ~/tmp/sample_text ## ## Usage: ## ## echo string | vix ... ## ## to give relative shift by such a Vigenere key... ## ################################################################ require "crypto_lib.pl"; $ref_file = "/home/faculty/garrett/tmp/nsmall"; @shift = @ARGV; @in = ; $in = join('',@in); open(FOO,"<$ref_file") || die "Can't open $ref_file\n"; @fin = ; close FOO; $fin = join('',@fin); $fin = lc($fin); $in = lc($in); $fin =~ s/[^a-z]+//g; ## now it's all lowercase sans spaces $in =~ s/[^a-z]+//g; ## now it's all lowercase sans spaces @fin = split(//,$fin); @in = split(//,$in); if ($#fin <= $#in) { $length = $#fin; } else { $length = $#in; } foreach $shift (0..25) { $number_coincidences = 0; foreach $i (0..$length) { if ($in[$i] eq &shift_by($shift + @shift[$i % ($#shift+1)], $fin[$i])) { $number_coincidences++; } } $out = int(10000 * $number_coincidences/($length+1))/100; print "Coincidences with shift ".$shift." = ".$out."\n" if ($out > 4.7); } print "\n"; #####################################