What you've said kinda got me thinking about a script i saw once in my early days of programming. I had a bit of a hack and came up with this. Its pretty rough, and is only really proof of concept quality code.

The process is:

  1. create your perl script
  2. encrypt it
  3. stick it into a header
The script
print "Hello World!\n"; print "This is an example of encrypting a perl\n"; print " script and not having the source directly viewable\n";

The encryption "engine":

#!/usr/bin/perl -w use strict; use Crypt::CBC; use Data::Dumper; local*FH; open(FH, 'hello.pl'); my $plaintext; while (<FH>) { $plaintext .= $_; } close FH; my $cipher = new Crypt::CBC('hey jude!'); my $ciphertext = $cipher->encrypt_hex($plaintext); for (my$i=0;$i<length($ciphertext);$i++) { print "\n" if ($i%30 == 0); print substr($ciphertext, $i,1); }

And now the header:

#!/usr/bin/perl -w use strict; use Crypt::CBC; use Data::Dumper; my $ciphertext; while (<DATA>) { chomp; $ciphertext .= $_; } my $cipher = new Crypt::CBC('hey jude!'); my $plaintext = $cipher->decrypt_hex($ciphertext); print "Plaintext\n"; print "---------\n"; print "$plaintext\n\n"; print "Eval:\n"; print "-----\n"; eval $plaintext; __DATA__ 52616e646f6d4956b1de8bd854dc4f 7137bd168ab1271410ce41442407f1 aa99ff61f79b89ba7ecfca35f283f7 cd5623b70aca91aedaef5a6bb1a7f5 343e40d1973d41720dab8105623d86 d0ed903db80073d57f8148ad799647 b947ae386b327dc61488d6e16392c6 9d623d1ac1f7fd0c767f182225ce6b 66a05c247903a2321e8a737bb3da4a fb5cf611dacbed89347997ab2db220 b1df993e95e7e0729405d84261bad4

The result:

[coolness@ryszard encrypt]# ./header.pl Plaintext --------- print "Hello World!\n"; print "This is an example of encrypting a perl\n"; print " script and not having the source directly viewable\n" Eval: ----- Hello World! This is an example of encrypting a perl script and not having the source directly viewable

Out of interest i ran it thru deparse and found:

[coolness@ryszard encrypt]# perl -MO=Deparse header.pl BEGIN { $^W = 1; } use Crypt::CBC; use Data::Dumper; use strict 'refs'; my $ciphertext; while (defined($_ = <DATA>)) { chomp $_; $ciphertext .= $_; } my $cipher = 'Crypt::CBC'->new('hey jude!'); my $plaintext = $cipher->decrypt_hex($ciphertext); print "Plaintext\n"; print "---------\n"; print "$plaintext\n\n"; print "Eval:\n"; print "-----\n"; eval $plaintext; __DATA__ 52616e646f6d4956b1de8bd854dc4f 7137bd168ab1271410ce41442407f1 aa99ff61f79b89ba7ecfca35f283f7 cd5623b70aca91aedaef5a6bb1a7f5 343e40d1973d41720dab8105623d86 d0ed903db80073d57f8148ad799647 b947ae386b327dc61488d6e16392c6 9d623d1ac1f7fd0c767f182225ce6b 66a05c247903a2321e8a737bb3da4a fb5cf611dacbed89347997ab2db220 b1df993e95e7e0729405d84261bad4 header.pl syntax OK

One thing i noticed while developing the above code was the last few characters of the source perl script seem to be gobbled up, resulting in the eval failing.. unfort, i dont have the time right now to fix it...

HTH

Update: and then there is always Filter::CBC - props to beatnik


In reply to Re: Encrypted Perl? by Ryszard
in thread Encrypted Perl? by jens

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.