Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 14:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found