#!/usr/bin/perl # cryptoa - revised 9/8/07 # kurt theis # use rc4 as stream generator to en/decrypt file # works on both *unix and Windows NT varients (XP, etc) #make sure there are no inconsistancies between versions/op sys etc. $digest = substr((crypt("cryptoa",12)),2); if ($digest ne "4WIS3iP/du2") { die "Incompatable version of Perl - bad crypt()\n"; } $ARGCOUNT = $#ARGV + 1; if ($ARGCOUNT == 0) { usage(); } # show usage and exit if ($ARGV[0] eq "?") { usage(); } # show usage and exit # get file name to work on $filename = $ARGV[0]; # open the files open(INFILE, $filename) || die "Error opening file $filename - $! \n"; open(OUTFILE, ">_$filename") || die "Error opening file $filename - $! \n"; binmode(INFILE); binmode(OUTFILE); # get a password use Term::ReadKey; print "Password: "; ReadMode('noecho'); $pword = ReadLine(0); if ($ENV{"OS"} ne "Windows_NT") { system("stty sane"); # do this for *nix boxes } chomp($pword); $password = substr((crypt($pword,12)),2); # remove salt # set up initial key structure based on password init_rc4(); while () { $line = $_; chop($line); $length = length($line); $count = 0; while ($count <= $length) { $char = substr($_,($count++),1); $enchar = $char ^ (chr rc4()); print OUTFILE $enchar; } if (eof(INFILE) == 1) { last; } } # clean up close(INFILE); close(OUTFILE); # rename original file to newly created if ($ENV{"OS"} eq "Windows_NT") { # windows command $cmd = "move _$filename $filename"; system($cmd) ==0 || die "Error changing file $filename - $! \n"; } else { # unix command $cmd = "mv _$filename $filename"; system($cmd) == 0 || die "Error changing file $filename - $! \n"; } # fini print "\nDone.\n"; exit; # ---------------- Subroutines Here ------------------ sub usage { print "\nUsage: cryptoa [filename]\n"; exit; } sub rc4 { $i = ($i+1) % 256; $j = ($j + $s[$i]) % 256; my $temp = $s[$i]; $s[$i] = $s[$j]; $s[$j] = $temp; $t = ($s[$i] + $s[$j]) % 256; # print "\nin rc4() i=$i j=$j val=$s[$t]"; return $s[$t]; } sub init_rc4 { for ($n=0; $n<256; $n++) { $s[$n] = $n; } $j = 0; my $len = length($password); for ($n=0; $n<256; $n++) { $k[$n] = ord(substr($password,($n % $len),1)); } for ($i=0; $i<256; $i++) { $j = ($j + $s[$i] + $k[$i]) % 256; my $temp = $s[$i]; $s[$i] = $s[$j]; $s[$j] = $temp; } $i = 1; $j = 1; }