http://qs1969.pair.com?node_id=164937


in reply to Encrypting script (was Encripting scipt)

Amplifying on the previous answers: you can't.

However, if you can restrict access to the environment in which your scripts run, you can 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 exactly the same as using Acme::Bleach, only keeping decryption knowledge for everything localised at a single place.

Put the password "<kbd>foobar</kbd>" (without the quotes) in your environment variable <samp>PASSWD</samp> (I told you it was laughably insecure!), and execute this 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`
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.

Here's a program to "encrypt". Again, put your password in envariable <samp>PASSWD</samp>.

#!/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->finis +h; __END__ END_HEADER $hdr =~ s/^ {4}//mg; print $hdr; while (<>) { print pack 'u', $cr->crypt($_); } print pack 'u', $cr->finish;

Adding encryption for modules is "left as an exercise for the interested reader" (i.e. I'm too indolent to do something so useless).

Finally, I cannot stress this enough: you get very little security from this sort of thing. 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.