Sunday, December 16, 2007

Simple Encryption and Decryption For Fun And No Profit

Here's another little fun thing for the weekend. It's based on Perl's pack and unpack functions and is a good introduction to using them for other purposes. It's also a good way to rediscover the fun of "passing notes in class," even though you're older and have a real job now ;)

You can use this script any way you want to; in fact it's written with a few things left out (how you want to deal with shell special characters -- another complete post on its own -- and if you want to expand on it to read and/or write to STDIN/STDOUT so you can pipe one instance to another, even though that defeats the purpose to a certain degree).

Hopefully, it will pique your curiosity about Perl and its ability to compress and convert different data types (I'm using hexadecimal and character here, but the available list is voluminous).

I've included usage points in the comments section of the script, but the basic usage would be: encode your message here decode 458616e6b6370264f62702659637964796e6760245865602c496e657870216e6460255e6968702d456e6167656279656
<-- The Hex output from a message encoded with this script.

Enjoy, and have a safe Sunday :)

Creative Commons License

This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License


# 2007 - Mike Golvach -
# Sanitize shell special characters in
# whatever manner you prefer - or just
# backslash them on the command line :)
# Sample usage:
# encode hi there
# encode hi there >FILE
# encode `cat FILE`
# decode 8696024786562756
# decode `cat FILE`
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

$input = $ARGV[0];
shift @ARGV;
$message = join(" ",@ARGV);

if ( $input eq "encode") {
print("$output \n");
} elsif ( $input eq "decode" ) {
print "\n$output\n";
} else {
print "Usage: $0 [encode|decode] whatever you want to type\n";

, Mike