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.


In reply to Re: Encrypting script by ariels
in thread Encrypting script (was Encripting scipt) by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.