| 1 |
#!perl |
| 2 |
require 5.002 ; |
| 3 |
|
| 4 |
use strict; |
| 5 |
use warnings; |
| 6 |
|
| 7 |
use vars qw($XOR $BLOCKSIZE $HEADERSIZE $CRYPT_MAGIC_1 $CRYPT_MAGIC_2 |
| 8 |
$size $mode $line $Fingerprint $file $block |
| 9 |
) ; |
| 10 |
|
| 11 |
$XOR = 'Perl' ; |
| 12 |
$BLOCKSIZE = length $XOR ; |
| 13 |
$HEADERSIZE = 2 ; |
| 14 |
$CRYPT_MAGIC_1 = 0xff ; |
| 15 |
$CRYPT_MAGIC_2 = 0x00 ; |
| 16 |
|
| 17 |
$Fingerprint = pack ("C*", $CRYPT_MAGIC_1, $CRYPT_MAGIC_2) ; |
| 18 |
|
| 19 |
die "Usage: encrypt file...\n" |
| 20 |
unless @ARGV ; |
| 21 |
|
| 22 |
# Loop throught each file in turn. |
| 23 |
foreach $file (@ARGV) |
| 24 |
{ |
| 25 |
|
| 26 |
if (! -T $file) |
| 27 |
{ |
| 28 |
print "Skipping directory $file\n" if -d $file ; |
| 29 |
print "Skipping non-text $file\n" if ! -d $file ; |
| 30 |
next ; |
| 31 |
} |
| 32 |
|
| 33 |
open (F, "<", $file) or die "Cannot open $file: $!\n" ; |
| 34 |
binmode F; |
| 35 |
open (O, ">", ${file}.".pe") or die "Cannot open ${file}.pe: $!\n" ; |
| 36 |
binmode O; |
| 37 |
|
| 38 |
# Get the mode |
| 39 |
$mode = (stat F)[2] ; |
| 40 |
|
| 41 |
# Check for "#!perl" line |
| 42 |
$line = <F> ; |
| 43 |
|
| 44 |
if ( $line =~ /^#!/ ) |
| 45 |
{ print O $line } |
| 46 |
else |
| 47 |
{ seek F, 0, 0 } |
| 48 |
|
| 49 |
print O "use Filter::decrypt ;\n" ; |
| 50 |
print O $Fingerprint ; |
| 51 |
|
| 52 |
|
| 53 |
$block = ''; |
| 54 |
while ($size = read(F, $block, $BLOCKSIZE) ) |
| 55 |
{ |
| 56 |
print O ($block ^ substr($XOR, 0, length $block)) ; |
| 57 |
} |
| 58 |
|
| 59 |
close F ; |
| 60 |
close O ; |
| 61 |
|
| 62 |
unlink ($file) |
| 63 |
or die "Could not remove '$file': $!\n" ; |
| 64 |
|
| 65 |
rename ("${file}.pe", $file) |
| 66 |
or die "Could not rename $file.pe to $file: $!\n" ; |
| 67 |
|
| 68 |
chmod $mode, $file unless $^O eq 'MSWin32' ; |
| 69 |
|
| 70 |
print "encrypted $file\n" ; |
| 71 |
} |
| 72 |
|