Crypt-CipherSaber-0.61/0040755000076400007640000000000007473745315013737 5ustar shaneshaneCrypt-CipherSaber-0.61/lib/0040755000076400007640000000000007473745315014505 5ustar shaneshaneCrypt-CipherSaber-0.61/lib/Crypt/0040755000076400007640000000000007473745315015606 5ustar shaneshaneCrypt-CipherSaber-0.61/lib/Crypt/CipherSaber.pm0100644000076400007640000001621607473745256020342 0ustar shaneshane################################################################################ # Crypt::CipherSaber # # an object oriented module implementing CipherSaber-1 and CS-2 # encryption # # copyright (c) 2001 - 2002 chromatic. All rights reserved. # This program is free software; you can distribute and modify it under the # same terms as Perl itself. ################################################################################ package Crypt::CipherSaber; use strict; use Carp; use vars qw($VERSION); $VERSION = '0.61'; sub new { my $class = shift; my $key = shift; # CS-2 shuffles the state array N times, CS-1 once my $N = shift; if (!(defined $N) or ($N < 1)) { $N = 1; } my $self = [ $key, [ 0 .. 255 ], $N ]; bless($self, $class); } sub crypt { my $self = shift; my $iv = shift; $self->_setup_key($iv); my $message = shift; my $state = $self->[1]; my $output = _do_crypt($state, $message); $self->[1] = [ 0 .. 255 ]; return $output; } sub encrypt { my $self = shift; my $iv = $self->_gen_iv(); return $iv . $self->crypt($iv, @_); } sub decrypt { my $self = shift; my ($iv, $message) = unpack("a10a*", +shift); return $self->crypt($iv, $message); } sub fh_crypt { my $self = shift; my ($in, $out, $iv) = @_; unless(UNIVERSAL::isa($in, 'GLOB') and UNIVERSAL::isa($out, 'GLOB')) { carp "I need filehandles! ($in) <$out>"; return; } local *OUT = $out; if (defined($iv)) { unless (length($iv) > 1) { $iv = $self->_gen_iv(); } $self->_setup_key($iv); print OUT $iv; } my $state = $self->[1]; my ($buf, @vars); while (<$in>) { unless ($iv) { ($iv, $_) = unpack("a10a*", $_); $self->_setup_key($iv); } my $line; ($line, $state, @vars) = _do_crypt($state, $_, @vars); print OUT $line if defined $line; } $self->[1] = [ 0 .. 255 ]; return 1; } ################### # # PRIVATE METHODS # ################### sub _gen_iv { my $iv; for (1 .. 10) { $iv .= chr(int(rand(256))); } return $iv; } sub _setup_key { my $self = shift; my $key = $self->[0] . shift; my @key = map { ord } split(//, $key); my $state = $self->[1]; my $j = 0; my $length = @key; # repeat N times, for CS-2 for (1 .. $self->[2]) { for my $i (0 .. 255) { $j += ($state->[$i] + ($key[$i % $length])); $j %= 256; (@$state[$i, $j]) = (@$state[$j, $i]); } } } sub _do_crypt { my ($state, $message, $i, $j, $n) = @_; my $output; for (0 .. (length($message) - 1 )) { $i++; $i %= 256; $j += $state->[$i]; $j %= 256; @$state[$i, $j] = @$state[$j, $i]; $n = $state->[$i] + $state->[$j]; $n %= 256; $output .= chr( $state->[$n] ^ ord(substr($message, $_, 1)) ); } return wantarray ? ($output, $state, $i, $j, $n) : $output; } 1; __END__ =head1 NAME Crypt::CipherSaber - Perl module implementing CipherSaber encryption. =head1 SYNOPSIS use Crypt::CipherSaber; my $cs = Crypt::CipherSaber->new('my pathetic secret key'); my $coded = $cs->encrypt('Here is a secret message for you'); my $decoded = $cs->decrypt($coded); # encrypt from and to a file open(INFILE, 'secretletter.txt') or die "Can't open infile: $!"; open(OUTFILE, '>secretletter.cs1') or die "Can't open outfile: $!"; binmode(INFILE); binmode(OUTFILE); $cs->fh_crypt(\*INFILE, \*OUTFILE, 1); # decrypt from and to a file open(INFILE, 'secretletter.cs1') or die "Can't open infile: $!"; open(OUTFILE, '>secretletter.txt') or die "Can't open outfile: $!"; binmode(INFILE); binmode(OUTFILE); $cs->fh_crypt(\*INFILE, \*OUTFILE); =head1 DESCRIPTION The Crypt::CipherSaber module implements CipherSaber encryption, described at http://ciphersaber.gurus.com. It is simple, fairly speedy, and relatively secure algorithm based on RC4. Encryption and decryption are done based on a secret key, which must be shared with all intended recipients of a message. =head1 METHODS =over =item B Initialize a new Crypt::CipherSaber object. $key, the key used to encrypt or to decrypt messages is required. $N is optional. If provided and greater than one, it will implement CipherSaber-2 encryption (slightly slower but more secure). If not specified, or equal to 1, the module defaults to CipherSaber-1 encryption. $N must be a positive integer greater than one. =item B Encrypt a message. This uses the key stored in the current Crypt::CipherSaber object. It will generate a 10-byte random IV (Initialization Vector) automatically, as defined in the RC4 specification. This returns a string containing the encrypted message. Note that the encrypted message may contain unprintable characters, as it uses the extended ASCII character set (valid numbers 0 through 255). =item B Decrypt a message. For the curious, the first ten bytes of an encrypted message are the IV, so this must strip it off first. This returns a string containing the decrypted message. The decrypted message may also contain unprintable characters, as the CipherSaber encryption scheme can handle binary files with fair ease. If this is important to you, be sure to treat the results correctly. =item B If you wish to generate the IV with a more cryptographically secure random string (at least compared to Perl's builtin rand() function), you may do so separately, passing it to this method directly. The IV must be a ten-byte string consisting of characters from the extended ASCII set. This is generally only useful for encryption, although you may extract the first ten characters of an encrypted message and pass them in yourself. You might as well call B, though. The more random the IV, the stronger the encryption tends to be. On some operating systems, you can read from /dev/random. Other approaches are the Math::TrulyRandom module, or compressing a file, removing the headers, and compressing it again. =item B For the sake of efficiency, Crypt::CipherSaber can now operate on filehandles. It's not super brilliant, but it's relatively fast and sane. Pass in a reference to the input file handle and the output filehandle. If your platform needs to use C, this is your responsibility. It is also your responsibility to close the files. You may also pass in an optional third parameter, an IV. There are three possibilities here. If you pass no IV, C will pull the first ten bytes from *INPUT and use that as an IV. This corresponds to decryption. If you pass in an IV of your own (generally ten digits, but more than one digits as the code is now), it will use your own IV when encrypting the file. If you pass in the value '1', it will generate a new, random IV for you. This corresponds to an encryption. =back =head1 COPYRIGHT AND LICENSE Copyright (C) 2000 - 2001 chromatic This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 AUTHOR chromatic thanks to jlp for testing, moral support, and never fearing the icky details and to the fine folks at http://perlmonks.org Additional thanks to Olivier Salaun and the Sympa project (http://www.sympa.org) for testing. =head1 SEE ALSO the CipherSaber home page at http://ciphersaber.gurus.com perl(1), rand(). =cut Crypt-CipherSaber-0.61/t/0040755000076400007640000000000007473745315014202 5ustar shaneshaneCrypt-CipherSaber-0.61/t/smiles.png0100644000076400007640000000260507473745274016210 0ustar shaneshanePNG  IHDR gAMA abKGD pHYs  ~tIMEkIDATxoKwgfؑ%!J H)%P)=B TPHHХ,d>sCIro=JywsfM7 UR,8RORYeYF9s 4c_sr(vvv8!cR$IjڵkSSSZͲ4,VJQJ s!j!}Wy>_0 ?<Ri"(܌X,Z1_o߾}̙Ruׯ_{>2 Cp8kѣG>|rΥph4߿ΝZ |q]u]16`kkk8fY&R !ϯ\AEi2ƄϷZ-qyի}M%p8B@!r|uP4M)1!B$E(oPZ҉93}j}`Y|iRinnX,ew" $;o8/IENDB`Crypt-CipherSaber-0.61/t/fh_encrypt.t0100644000076400007640000000364507263707202016523 0ustar shaneshane# tests the fh_encrypt() method # this will fail if the state array is not reinitialized ... oops! use Crypt::CipherSaber; use File::Spec; my $crypt_in = File::Spec->catfile('t', 'smiles.cs1'); my $crypt_out = File::Spec->catfile('t', 'outsmiles.cs1'); my $img_in = File::Spec->catfile('t', 'smiles.png'); my $img_out = File::Spec->catfile('t', 'outsmiles.png'); print "1..4\n"; open(IN, $crypt_in) or die "Can't get IV!\n"; binmode(IN); my $iv = unpack("a10", ); # encrypt a message my $cs = Crypt::CipherSaber->new('sdrawkcabsihtdaeR'); open(IN, $img_in) or die "Can't open input file!\n"; open(OUT, '>'. $crypt_out) or die "Can't open output file!\n"; binmode(IN); binmode(OUT); if ($cs->fh_crypt(\*IN, \*OUT, $iv)) { print "ok 1\n"; } else { print "not ok 1\n"; } open(ENCRYPTED, $crypt_out) or die "Can't open encrypted file!\n"; open(FIXED, $crypt_in) or die "Can't open fixed file!\n"; binmode(ENCRYPTED); binmode(FIXED); my $status = 0; while () { my $fixed = ; my $pos = 0; for my $char (split(//, $_)) { next if (substr($fixed, $pos++, 1) eq $char); $status = 1; } last if $status; } if ($status == 0) { print "ok 2\n"; } else { print "not ok 2\n"; } open(IN, $crypt_in) or die "Can't open input file 2!\n"; open(OUT, '>' . $img_out) or die "Can't open output file 2!\n"; binmode(IN); binmode(OUT); if ($cs->fh_crypt(\*IN, \*OUT)) { print "ok 3\n"; } else { print "not ok 3\n"; } close(IN); close(OUT); open(ENCRYPTED, $img_out) or die "Can't open encrypted file!\n"; open(FIXED, $img_in) or die "Can't open fixed file!\n"; binmode(ENCRYPTED); binmode(FIXED); $status = 0; while () { my $fixed = ; my $pos = 0; for my $char (split(//, $_)) { next if (substr($fixed, $pos++, 1) eq $char); $status = 1; print STDERR "Error at line $.\n"; last; } last if $status; } close(ENCRYPTED); close(FIXED); if ($status == 0) { print "ok 4\n"; } else { print "not ok 4\n"; } Crypt-CipherSaber-0.61/t/create.t0100644000076400007640000000032107212055572015611 0ustar shaneshaneuse Crypt::CipherSaber; print "1..1\n"; # first, try to create an object my $cs = Crypt::CipherSaber->new('first key') or die "not ok 2"; if (defined($cs)) { print "ok 1\n"; } else { print "not ok 1\n"; } Crypt-CipherSaber-0.61/t/CS2.t0100644000076400007640000000051307263246135014743 0ustar shaneshane# now do a bidirectional check with CS-2 use Crypt::CipherSaber; print "1..1\n"; my $cs2 = Crypt::CipherSaber->new('second key', 5); my $long_line = join(' ', (1 .. 100)); my $coded = $cs2->encrypt($long_line); my $decoded = $cs2->decrypt($coded); if ($long_line eq $decoded) { print "ok 1\n"; } else { print "not ok 1\n"; } Crypt-CipherSaber-0.61/t/both_long.t0100644000076400007640000000057207212055563016331 0ustar shaneshane# encrypt and decrypt a line greater than 256 characters long # this tests for a subtle bug, ie, missing a modulo on $i use Crypt::CipherSaber; print "1..1\n"; my $cs = new Crypt::CipherSaber ('first key'); my $long_line = join(' ', (1 .. 100)); my $coded = $cs->encrypt($long_line); if ($long_line = $cs->decrypt($coded)) { print "ok 1\n"; } else { print "not ok 1\n"; } Crypt-CipherSaber-0.61/t/encrypt.t0100644000076400007640000000117007263246101016031 0ustar shaneshaneuse Crypt::CipherSaber; print "1..2\n"; # encrypt a message my $cs = Crypt::CipherSaber->new('asdfg'); my $coded = $cs->crypt('abcdefghij', 'This is another test.'); my $message = join('', map { chr } qw ( 153 90 51 37 126 114 217 0 50 245 103 36 219 18 4 44 169 53 32 64 15 )); if ($coded eq $message) { print "ok 1\n"; } else { print "not ok 1\n"; } # decrypt a previously encrypted message $message = join('', map{ chr } qw( 99 228 225 111 163 246 142 168 143 125 239 199 167 58 192 81 211 122 19 200 97 57 101 151 19 )); if ($cs->decrypt($message) eq 'This is a test.') { print "ok 2\n"; } else { print "not ok 2\n"; } Crypt-CipherSaber-0.61/t/smiles.cs10100644000076400007640000000261707263707601016102 0ustar shaneshaneKM,\TKƢmC'ʷ%N#@~e\< ﮽x96fƻ~W` Y%ϵV2D${"fJ w9'>"T/l986 ]Oi%Y etx]!4ads"1تz0Mͩ`|0.8g" C~ ;T|E'll8`ƔƆ!;ug a*A1&93}WTl (n?%4vu/7f]X4Bʖo`ەBqj+!]Ln kNhӘ3"w#P߳Ș#:|7 }?R;+mc*l 0䂇4O ٻsҫ`ObKiK˚`w@nQo8F֬'rpN T˩%)Y`<=1#]9E)7DbNIkw81vncn)(jL=ogZXՠogZ@qԴz|_|glga}}dV(Uğb y"lDd^GO6$7{)]qou1#POَEh_CKWڵpx&sG4@o ԫKzar)_hy WXFn'5eKOaFU` Sn])žG~rBwV^+(\@#@TmsL2f!sI{Z0tY&ܲ{2Urن| ĶsXhz;s7&Ni~:\v*k=֡WbREHe@ϝk  NgGY:Q_XTO_x^q,iSEoA F>?G"'v&OYM\Ό29+!7`(%FP@GMd-/JĞ0I-8dnNf2 Iӑss݆͎%MpCJ2䜆O ^;lmc!?Ȝ9fkCrypt-CipherSaber-0.61/t/bigfile.t0100644000076400007640000000122507263707640015761 0ustar shaneshane# test to see if we can decrypt files via filehandles # this tests for a subtle bug, ie, missing a modulo on $i use Crypt::CipherSaber; use File::Spec; print "1..1\n"; my $cs = new Crypt::CipherSaber ('sdrawkcabsihtdaeR'); open(INPUT, File::Spec->catfile('t', 'smiles.cs1')) or die "Couldn't open: $!"; binmode(INPUT); open(OUTPUT, '>' . File::Spec->catfile('t', 'smiles.png')) or die "Couldn't open: $!"; binmode(OUTPUT); $cs->fh_crypt(\*INPUT, \*OUTPUT); close INPUT; close OUTPUT; open(TEST, File::Spec->catfile('t', 'smiles.png')) or die "Couldn't open: $!"; my $line = ; if ($line =~ /PNG/) { print "ok 1\n"; } else { print "not ok 1\n"; } Crypt-CipherSaber-0.61/README0100644000076400007640000000212007263711060014572 0ustar shaneshaneCrypt::CipherSaber ------------------ version 0.60 released 7 April 2001 Crypt::CipherSaber is a Perl module providing an object oriented interface to CipherSaber-1 and CipherSaber-2 encryption. See the POD for further details on use. See http://ciphersaber.gurus.com for more information about CipherSaber. After unpacking the tarball, to install this module type: perl Makefile.PL make make test make install The encryption itself is simple, relatively fast, and fairly secure. It uses a shared secret system, and is suitable even for binary files. It should run without modification anywhere Perl runs. Prerequisites: none Recent changes: - works on Perl 5.004 and later (0.60) - supports filehandle-to-filehandle encryption and decryption (0.60) Enhancements/plans: * built-in support for a better randomization scheme? * support changing keys in an object rather than creating a new one? copyright (c) 2001 chromatic (chromatic@wgz.org), all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Crypt-CipherSaber-0.61/MANIFEST0100644000076400007640000000023707263711261015055 0ustar shaneshaneChanges lib/Crypt/CipherSaber.pm Makefile.PL MANIFEST README t/CS2.t t/bigfile.t t/both_long.t t/create.t t/encrypt.t t/fh_encrypt.t t/smiles.cs1 t/smiles.png Crypt-CipherSaber-0.61/Changes0100644000076400007640000000155107473745216015231 0ustar shaneshaneRevision history for Perl extension Crypt::CipherSaber. 0.61 Sat May 25 17:31:52 UTC 2002 - avoid uninitialized value warnings in fh_crypt() Thu May 10 2001 - fixed _gen_iv() to generate 255 characters (thanks to John Wiersba) Sun Apr 29 2001 - added license/copyright information to the pod in the module itself 0.60 Sat Apr 7 2001 - some internal cleanup - added some comments - added documentation for fh_crypt() Thu Apr 5 2001 - made _gen_iv() work with Perl 5.004 (again Sympa) - added fh_crypt() - added _do_crypt() to help with fh_crypt() - made crypt() use _do_crypt() - added t/bigfile.t and t/fh_encrypt.t 0.51 Wed Apr 4 2001 UNRELEASED VERSION - made decrypt() work with Perl 5.004 (for Sympa project) 0.50 Fri Dec 1 17:02:02 2000 - original version; created by h2xs 1.19 Crypt-CipherSaber-0.61/Makefile.PL0100644000076400007640000000040207212047440015663 0ustar shaneshaneuse ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. WriteMakefile( 'NAME' => 'Crypt::CipherSaber', 'VERSION_FROM' => 'lib/Crypt/CipherSaber.pm', # finds $VERSION );