Crypt-CipherSaber

 view release on metacpan or  search on metacpan

lib/Crypt/CipherSaber.pm  view on Meta::CPAN

    }
    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 sad 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 my $in,       'secretletter.txt' or die "Can't open infile: $!";
  open my $out, '>', 'secretletter.cs1' or die "Can't open outfile: $!";
  binmode $in;
  binmode $out;

  $cs->fh_crypt($in, $out, 1);

  # decrypt from and to a file
  open my $in,       'secretletter.txt' or die "Can't open infile: $!";
  open my $out, '>', 'secretletter.cs1' or die "Can't open outfile: $!";

  binmode $in;
  binmode $out;
  $cs->fh_crypt($in, $out);

=head1 DESCRIPTION

The Crypt::CipherSaber module implements CipherSaber encryption, described at
L<http://ciphersaber.gurus.com/>.  It is simple, fairly speedy, and relatively
secure algorithm based on RC4. I<Relatively>, given 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<new($key, $N)>

Initialize a new Crypt::CipherSaber object.  C<$key> is a required parameter:
the key used to encrypt or to decrypt messages.  C<$N> is optional.  If
provided and greater than one, it causes the object to use CipherSaber-2
encryption (slightly slower but more secure).  If not specified, or equal to 1,
the module defaults to CipherSaber-1 encryption.  C<$N> must be a positive
integer greater than one.

=item B<encrypt($message)>

Encrypt a message.  This uses the key stored in the current Crypt::CipherSaber
object.  It generates 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($message)>

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 handles binary filesIf this is important to you,
be sure to treat the results correctly.

=item B<crypt($iv, $message)>



( run in 0.983 second using v1.01-cache-2.11-cpan-bbe5e583499 )