sourcecode
beretboy
<CODE>
#!/usr/bin/perl
use strict;
use Term::ReadPassword;
sub data {
my $file = shift;
$/ = undef;
open(PDATA,$file) || die "Input File Not Found!";
my $data = <PDATA>;
my @data = split("", $data);
@data = map(ord, @data);
$/ = "\n";
close(PDATA);
return @data;
}
sub passphrase {
my $p;
my $p = read_password('PassPhrase:');
my @p = split("",$p);
@p = map(ord, @p);
return @p;
}
$ARGV[2] || die "Usage: $0 <input file> <output file> <mode (e)ncrypt (d)ecrypt>\n";
my $mode = $ARGV[2];
my @state = 0..255;
my $file = $ARGV[0];
my @data = data($file);
my (@iv,$iv);
if ($mode eq "d") {
@iv = splice(@data,0,10);;
} else {
open(RANDOM,"/dev/urandom") || die "Can't Find Random Source";
read(RANDOM,$iv,10);
close(RANDOM);
@iv = split("",$iv);
@iv = map(ord, @iv);
}
my @key = passphrase;
my $key_length = push(@key,@iv);
my $n;
my $a = 0;
my $b = 0;
for ($n = 0; $n <= 255; $n++) {
$b = ($b + $key[$a] + $state[$n]) % 256;
($state[$n],$state[$b]) = ($state[$b],$state[$n]);
$a = ($a + 1) % $key_length;
}
$a = 0;
$b = 0;
my $data_length = @data;
$data_length--;
my $c;
my $i;
my $out = $ARGV[1];
open(OUT,">$out");
if ($mode eq "e") {
print OUT $iv;
}
for ($c = 0; $c <= $data_length; $c++) {
$a = ($a + 1) % 256;
$b = ($b + $state[$a]) % 256;
($state[$a],$state[$b]) = ($state[$b],$state[$a]);
$i = ($state[$a] + $state[$b]) % 256;
my $byte = chr $data[$c];
my $cbyte = chr $state[$i];
my $obyte = $byte ^ $cbyte;
print OUT $obyte;
}
</CODE>
This is my own personal implementation of <A HREF="http://ciphersaber.gurus.com/">CipherSaber</A>. It adheres to the CipherSaber-1 standard. It is slow, but if you want speed, use a C version.
Cryptography
beretboy@perlmonk.org
Bjorn Joseph Westergard