#!/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;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|