Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

CipherSaber

by beretboy (Chaplain)
on Jun 27, 2003 at 19:44 UTC ( [id://269714]=sourcecode: print w/replies, xml ) Need Help??
Category: Cryptography
Author/Contact Info beretboy@perlmonk.org Bjorn Joseph Westergard
Description: This is my own personal implementation of CipherSaber. It adheres to the CipherSaber-1 standard. It is slow, but if you want speed, use a C version.
#!/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;
}
Replies are listed 'Best First'.
Re: CipherSaber
by chromatic (Archbishop) on Jun 28, 2003 at 00:54 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://269714]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-03-28 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found