Created
December 17, 2011 22:12
-
-
Save codeswimmer/1491569 to your computer and use it in GitHub Desktop.
perl: Chaocipher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ChaoSim.pl : Simulation of Chaocipher enciphering/deciphering | |
# (c) Moshe Rubin, August 2010 | |
# email: mosher@mountainvistasoft.com | |
use strict; | |
use diagnostics; | |
use warnings; | |
my $left = uc($ARGV[1]); | |
my $right = uc($ARGV[2]); | |
my $mode = $ARGV[3]; | |
my $pt = ""; | |
my $ct = ""; | |
my $len = 0; | |
my $trace = 0; | |
if (scalar(@ARGV) == 0) | |
{ | |
usage(); | |
exit (-1); | |
} | |
if (exists ($ARGV[4])) | |
{ | |
$trace = 1; | |
} | |
# Read input file | |
if ($mode eq "encipher") | |
{ | |
$pt = getFile ($ARGV[0]); | |
$len = length($pt); | |
} | |
else | |
{ | |
$ct = getFile ($ARGV[0]); | |
$len = length($ct); | |
} | |
printf "\n"; | |
printf "Left: $left Right: $right"; | |
for (my $i=0; $i<$len; ++$i) | |
{ | |
my $p; | |
my $c; | |
my $shift; | |
if ($mode eq "encipher") | |
{ | |
# Encipher plaintext letter | |
$p = substr ($pt, $i, 1); | |
($right, $shift) = bringToZenith ($right, $p); | |
$left = rotate ($left, $shift); | |
$c = substr ($left, 0, 1); | |
$ct .= $c; | |
} | |
else | |
{ | |
# Decipher ciphertext letter | |
$c = substr ($ct, $i, 1); | |
($left, $shift) = bringToZenith ($left, $c); | |
$right = rotate ($right, $shift); | |
$p = substr ($right, 0, 1); | |
$pt .= $p; | |
} | |
printf " ($p,$c)\n" if $trace; | |
# Permute alphabets | |
$left = permute ($left, 1); | |
$right = rotate ($right, 1); | |
$right = permute ($right, 2); | |
printf "Left: $left Right: $right" if $trace; | |
}Chaocipher Revealed: The Algorithm 2 | |
printf "\n\n"; | |
printf "Plaintext: $pt\n"; | |
printf "\n"; | |
printf "Ciphertext: $ct\n"; | |
sub getFile | |
{ | |
my ($f) = @_; | |
my $text = ""; | |
my $line; | |
open (FILE, "<$f"); | |
while ($line = <FILE>) | |
{ | |
chomp($line); | |
$line =~ s/\s+//g; | |
$line = uc($line); | |
$text .= $line; | |
} | |
close (FILE); | |
return $text; | |
} | |
sub bringToZenith | |
{ | |
# Bring letter to zenith position | |
my ($alphabet, $letter) = @_; | |
my $index = index ($alphabet, $letter); | |
return (rotate ($alphabet, $index), $index); | |
} | |
sub rotate | |
{ | |
# Rotate alphabet N positions counterclockwise | |
my ($alphabet, $shift) = @_; | |
return ($shift > 0) ? | |
substr ($alphabet, $shift) . substr ($alphabet, 0, $shift) : | |
$alphabet; | |
} | |
sub permute | |
{ | |
# Generic Chaocipher alphabet permutation (i.e., Nick Pelling's "twizzling") | |
my ($alphabet, $offset) = @_; | |
return substr ($alphabet, 0, $offset) . | |
substr ($alphabet, $offset+1, 13-$offset) . | |
substr ($alphabet, $offset, 1) . | |
substr ($alphabet, 14); | |
} | |
sub usage | |
{ | |
printf "Usage: perl ChaoSim.pl <input_file> <left_alphabet> <right_alphabet>\n"; | |
printf " <'encipher' | 'decipher'> [trace]\n\n"; | |
printf "Example: perl ChaoSim.pl my.ct.txt emkxgdclirwpvqutnbjshyaozf\n"; | |
printf " zpjkelbohdtycavirufmnxgqsw decipher\n\n"; | |
printf " perl ChaoSim.pl my.pt.txt bfurkashexcymnvqzgijtldwpo\n"; | |
printf " uslfieavpdcybjzthogkmnxwqr encipher 1\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment