note
ariels
<p>
Amplifying on the previous answers: <big><big>you can't</big></big>.
</p>
<p>
However, if you can restrict access to the environment in which your scripts run, you <em>can</em> use some external key to decrypt them. Of course, if an attacker can lay hands on that key, you still lose. Think of it as <strong>exactly the same</strong> as using [Acme::Bleach], only keeping decryption knowledge for everything localised at a single place.
</p>
<p>
Put the password "<kbd>foobar</kbd>" (without the quotes) in your environment variable <samp>PASSWD</samp> <small>(I told you it was laughably insecure!)</small>, and execute this code:
<blockquote><code>
#!/usr/local/bin/perl
use warnings;
use strict;
use Crypt::CBC;
use Crypt::DES; # give compile-time error if uninstalled
use Digest::MD5 qw/md5/;
my $cr = new Crypt::CBC ({ key => $ENV{PASSWD},
cipher => 'DES',
});
$cr->start('decrypting');
eval join '', (map {$cr->crypt(unpack 'u',$_)} <DATA>), $cr->finish;
__END__
H4F%N9&]M258,7+K!Y.>&R#R(9V'IHIQ/V38VIC%,U54_2EA%]]#L````
(C4$VS`4H"\T`
8`^Z3::)M9OZC2NJ6!C!."#\1T+4:?`DE
M98',<EH%[_U>_#TF-U;OHK<?^0Q'/$EKW,MRB;.Z(A@*7Q4?MCQ=;^!QF/B0
CR/)<\*6UC+0R3[P*L>Y+YF\C6WGV7E["2:=,PKE^;*[?M`@`
(TCPFK5[11M8`
</code></blockquote>
Uses [Digest::MD5], [Crypt::DES] and [Crypt::CBC]. DES encryption really isn't considered particularly secure nowadays, but compared to the security you're getting from the rest of this writeup it's an impregnable fortress.
</p>
<p>
Here's a program to "encrypt". Again, put your password in envariable <samp>PASSWD</samp>.
<blockquote><code>
#!/usr/local/bin/perl
use warnings;
use strict;
use Crypt::CBC;
use Crypt::DES; # give compile-time error if uninstalled
use Digest::MD5 qw/md5/;
die "$0: Gimme a password in environment variable PASSWD\n"
unless exists $ENV{PASSWD};
my $cr = new Crypt::CBC ({ key => $ENV{PASSWD},
cipher => 'DES',
});
$cr->start('encrypting');
my $hdr = <<'END_HEADER';
#!/usr/local/bin/perl
use warnings;
use strict;
use Crypt::CBC;
use Crypt::DES; # give compile-time error if uninstalled
use Digest::MD5 qw/md5/;
my $cr = new Crypt::CBC ({ key => $ENV{PASSWD},
cipher => 'DES',
});
$cr->start('decrypting');
eval join '', (map {$cr->crypt(unpack 'u',$_)} <DATA>), $cr->finish;
__END__
END_HEADER
$hdr =~ s/^ {4}//mg;
print $hdr;
while (<>) {
print pack 'u', $cr->crypt($_);
}
print pack 'u', $cr->finish;
</code></blockquote>
</p>
<p>
Adding encryption for modules is "left as an exercise for the interested reader" <small>(i.e. I'm too indolent to do something so useless)</small>.
</p>
<p>
Finally, I cannot stress this enough: you get very little security from this sort of thing. <strong><big>If you need security, take a good hard look at what it is you're trying to do. Any encryption (including the superstars like AES, RSA, and any other TLA) is no stronger than the protection of its key. And if you want to run your code, you must provide access to that key.</big></strong>
</p>
164891
164891