Quite a few years ago, I was helping someone doing some light data manipulation stuff (obfuscation really). Premise was to obfu a string with a password, print out the result, and then to decipher it, you had to supply the same password and paste in the jumbled hex text. Here's an example of how it works, then the actual code. The code is in two files (for the convenience of the person I was helping; it could easily be merged. Also, if I were to actually use it myself, I'd add another input field for the salt as well (it's hard-coded), but I digress). This is very simple and probably far from secure, but thought I'd share anyhow.
Obfuscate a message:
perl enc.pl
Create a password: secretpw
Enter your message to be encrypted: This is an encryption test
Your encrypted message: c7cda0cc55bde684b5db9698d5d6d7b0c9a9bde2d274e1
+dba6db
Then, to decrypt:
perl denc.pl
Enter your password: secretpw
Enter your encrypted message: c7cda0cc55bde684b5db9698d5d6d7b0c9a9bde2
+d274e1dba6db
Your decrypted message: This is an encryption test
The enc.pl code:
use warnings;
use strict;
use 5.10.0;
print "\nCreate a password: ";
chomp ( my $password = <STDIN> );
my $salt = 'j4';
my $crypt_pass = crypt( $salt, $password );
print "Enter your message to be encrypted: ";
chomp ( my $message = <STDIN> );
my @hex_pass
= map { sprintf( "%x", ( ord( $_ ))) } split //, $crypt_pass;
my @hex_msg
= map { sprintf( "%x", ( ord( $_ ))) } split //, $message;
my @crypted;
my @hash;
push @hash, @hex_pass until @hash > @hex_msg;
my $i=0;
for my $letter ( @hex_msg ){
push @crypted, hex( $letter ) + hex( $hash[$i] );
$i++;
}
@crypted = map { sprintf( "%x", $_ ) } @crypted;
print "\nYour encrypted message: ";
print @crypted;
print "\n\n";
The denc.pl code:
use warnings;
use strict;
use 5.10.0;
print "\nEnter your password: ";
chomp ( my $password = <STDIN> );
my $salt = 'j4';
my $crypt_pass = crypt( $salt, $password );
my @hex_pass = map { sprintf( "%x", ( ord( $_ ) ) ) } split //, $crypt
+_pass;
print "Enter your encrypted message: ";
chomp ( my $message = <STDIN> );
my @crypt_message = ( $message =~ m/../g );
my @hash;
push @hash, @hex_pass until @hash > @crypt_message;
my @decrypted_message;
my $n = 0;
for my $letter ( @crypt_message ){
push @decrypted_message, ( hex( $letter ) - hex( $hash[$n] ) ) ;
$n++;
}
print "\nYour decrypted message: ";
print map { chr( $_ ) } @decrypted_message;
print "\n\n";
-
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.