| 1 | # $File: //member/autrijus/Module-CryptSource/CryptSource.pm $ $Author: autrijus $ | ||||||||||||||||||||||||||||||||
| 2 | # $Revision: #2 $ $Change: 685 $ $DateTime: 2002/08/17 08:59:09 $ | ||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | package Module::CryptSource; | ||||||||||||||||||||||||||||||||
| 5 | 1 | $Module::CryptSource::VERSION = '0.02'; | |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Storable qw(thaw nfreeze); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use MIME::Base64 qw(encode_base64 decode_base64); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Term::ReadKey qw(ReadMode); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Digest::MD5 qw(md5_hex); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Crypt::Rijndael; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | Module::CryptSource - Encrypt and Decrypt source for Binary Packagers | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | In myprog.pl: | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | use Module::CryptSource; | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | Afterwards: | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | % perl myprog.pl --ensrc | ||||||||||||||||||||||||||||||||
| 28 | % perlapp -f myprog.pl # or perl2exe | ||||||||||||||||||||||||||||||||
| 29 | % myprog.exe --desrc | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | One of the problems in binary packages such as perlapp or perl2exe is | ||||||||||||||||||||||||||||||||
| 34 | the difficulty of uncovering the source code. While it can be argued | ||||||||||||||||||||||||||||||||
| 35 | that obscuring source code may be necessary in some environments, it | ||||||||||||||||||||||||||||||||
| 36 | could be disastrous when you accidentally lose the source code. | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | This self-modifying module let you specify a key to encrypted source | ||||||||||||||||||||||||||||||||
| 39 | code and modules, so you can use that key to retrieve them back. | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | To use this module, simply put C<use Module::CryptSource;> in all scripts | ||||||||||||||||||||||||||||||||
| 42 | and modules you wish to encrypt. Run it once with the C<--ensrc> option | ||||||||||||||||||||||||||||||||
| 43 | before running B<perlapp> or B<perl2exe>, so it can store the modified | ||||||||||||||||||||||||||||||||
| 44 | F<Module/CryptSource.pm> along with your F<.exe> file; copies of encrypted | ||||||||||||||||||||||||||||||||
| 45 | source files will be stored in the F<Module/CryptSource.pm> file. | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | Run the resulting executable with C<--desrc> to decrypt the source files | ||||||||||||||||||||||||||||||||
| 48 | back. | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | =head1 CAVEATS | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | B<Storable>, B<Crypt::Rijndael> and B<Term::ReadKey> are three XS | ||||||||||||||||||||||||||||||||
| 53 | modules you'll have to install it yourself. It's probably possible | ||||||||||||||||||||||||||||||||
| 54 | to use B<Crypt::Rijndael_PP> and C<Win32::*> as alternatives, but | ||||||||||||||||||||||||||||||||
| 55 | I don't have the motivation to investigate yet. | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | If user enters a wrong decryption key, chances are that the | ||||||||||||||||||||||||||||||||
| 58 | C<unpack('N/a*')> will report an C<Out of memory!> error. | ||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 1 | my %Files; | |||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 1 | ▶ | sub import { | ||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=9 SCAR=38.6
| |||||||||||||||||||||||||||||||||
| 65 | 1 | ▶ | return $Files{(caller)[1]}++ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
not $0 =~ /\.exe$/i and ($ARGV[0] || "") eq "--ensrc"
$ARGV[0] || ''
MC/DC0%0/2
not $0 =~ /\.exe$/i and ($ARGV[0] || "") eq "--ensrc"
not $0 =~ /\.exe$/i
$ARGV[0]
Truth table33%1/3
not(A) and B
A= not $0 =~ /\.exe$/iB= $ARGV[0]
| |||||||||||||||||||||||||||||||||
| 66 | if ($0 !~ /\.exe$/i) and (($ARGV[0] || '') eq '--ensrc'); | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | return unless ($ARGV[0] || '') eq '--desrc'; local *FH; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$ARGV[0] || ''
MC/DC0%0/1
$ARGV[0] || ''
$ARGV[0]
Truth table50%1/2
A
A= $ARGV[0]
| |||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | %Files = %{+eval{thaw(unpack('N/a*', Crypt::Rijndael->new(md5_hex( | ||||||||||||||||||||||||||||||
|
Condition0%0/2
eval {
do {
thaw(unpack('N/a*', 'Crypt::Rijndael'->new(md5_hex(ReadMode(2), print('Enter the decryption key:'), scalar readline STDIN))->decrypt(&decode_base64(scalar &SRC()))))
}
} || die("\nDecryption failed!")
MC/DC0%0/1
eval {
do {
thaw(unpack('N/a*', 'Crypt::Rijndael'->new(md5_hex(ReadMode(2), print('Enter the decryption key:'), scalar readline STDIN))->decrypt(&decode_base64(scalar &SRC()))))
}
} || die("\nDecryption failed!")
eval {
do {
thaw(unpack('N/a*', 'Crypt::Rijndael'->new(md5_hex(ReadMode(2), print('Enter the decryption key:'), scalar readline STDIN))->decrypt(&decode_base64(scalar &SRC()))))
}
}
Truth table0%0/2
A
A= eval {
do {
thaw(unpack('N/a*', 'Crypt::Rijndael'->new(md5_hex(ReadMode(2), print('Enter the decryption key:'), scalar readline STDIN))->decrypt(&decode_base64(scalar &SRC()))))
}
}
| |||||||||||||||||||||||||||||||||
| 71 | ReadMode(2), print("Enter the decryption key:"), scalar <STDIN> | ||||||||||||||||||||||||||||||||
| 72 | ))->decrypt(decode_base64(SRC()))))} or die "\nDecryption failed!"}; | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | while (my ($file, $src) = each %Files) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | 0 | print "\nDecrypting $file..."; | |||||||||||||||||||||||||||||||
| 76 | 0 | chmod 0644, $file; | |||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | open FH, ">$file" or (warn("Cannot write to $file: $!"), next); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 78 | 0 | print FH $src; | |||||||||||||||||||||||||||||||
| 79 | 0 | close FH; | |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 0 | %Files = (); print "\n"; exit; | |||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | INIT { if (%Files) { | ||||||||||||||||||||||||||||||||
| 86 | local *FH; | ||||||||||||||||||||||||||||||||
| 87 | my $src = do { local $/; open FH, __FILE__; <FH> }; | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | $src =~ s[# <!--\n.*\n# -->]["# <!--\n". | ||||||||||||||||||||||||||||||||
| 90 | encode_base64(Crypt::Rijndael->new(md5_hex( | ||||||||||||||||||||||||||||||||
| 91 | ReadMode(2), print("Enter the encryption key:"), scalar <STDIN> | ||||||||||||||||||||||||||||||||
| 92 | ))->encrypt(do{ | ||||||||||||||||||||||||||||||||
| 93 | my $d = pack('N/a*', nfreeze({map{ | ||||||||||||||||||||||||||||||||
| 94 | print "\nEncrypting $_..."; | ||||||||||||||||||||||||||||||||
| 95 | local $/; open FH, $_; $_ => <FH>; | ||||||||||||||||||||||||||||||||
| 96 | } keys %Files})); | ||||||||||||||||||||||||||||||||
| 97 | $d . (' ' x (-length($d) % 16)); | ||||||||||||||||||||||||||||||||
| 98 | })).".\n# -->"]es; die "Encryption failed: $@" if $@; | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | chmod 0644, __FILE__; | ||||||||||||||||||||||||||||||||
| 101 | open FH, ">".__FILE__ or die $!; | ||||||||||||||||||||||||||||||||
| 102 | print FH $src; | ||||||||||||||||||||||||||||||||
| 103 | close FH; | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | print "\n"; exit; | ||||||||||||||||||||||||||||||||
| 106 | } }; | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | 1 | ▶ | use constant SRC => (($ARGV[0] || '') ne '--desrc') ? '' : << '.'; # <!-- | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=11.7
Branch: 1 branch
Condition50%1/2
$ARGV[0] || ''
MC/DC0%0/1
$ARGV[0] || ''
$ARGV[0]
Truth table50%1/2
A
A= $ARGV[0]
| |||||||||||||||||||||||||||||||||
| 109 | . | ||||||||||||||||||||||||||||||||
| 110 | # --> | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | 1 | 1; | |||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | __END__ | ||||||||||||||||||||||||||||||||