postimg
Mar 2010 15

Make 007 Jealous With RSA Encryption2

Posted In Miscellaneous,Perl By John Hass

Make 007 Jealous

Public/Private encryption is not a new thing, did you know you can use my favorite swiss army knife to send secret messages to your friends…. Scratch that, I don’t have any friends… Perl Makes this easy with a little module calls Crypt::RSA, yes RSA is under fire for security blah blah, I don’t care, it’s still secure, because Mr. S’s (yes it’s a dudes name) I can’t pronounce nor spell, I know it will be fairly decent, I am almost about sure Mr. S should be Dr. S, but that just sounds evil, so without further ado, public/private key encryption with perl.

Two people in this Article Tony and John, John wants all correspondence encrypted Tony Encrypts.

I am on my new Mac now, so your mileage may very….

sudo perl -MCPAN -e shell
install Crypt::RSA

it asks you about a ton of dependencies, don’t do what your high school guidance counselor told you and just say yes.

Create something to make the keys

#!/usr/bin/perl
use Crypt::RSA;
$rsa = new Crypt::RSA;
if ($ARGV[0] eq "--makekeys") {
	($public, $private) = $rsa->keygen( 
				Size => 2048,
				Filename => "keylock",
				);
	exit;
}

The start of out program generate our rsa key, keylock.private (and keep this private, no touchy no sendy) and keylock.private, send this to everyone

ok send the public key to tony, so he can send us a message…. Tony should create the message now

if ($ARGV[0] eq "--createmessage") {
	$key = $ARGV[1];
	$message = $ARGV[2];
 
	#open the key
	$akey = new Crypt::RSA::Key::Public (	Filename => "$key");
	my $output = $rsa->encrypt(Message => $message,Key=> $akey)|| die $rsa->errstr();
	open ENC, ">message" or die $!;
	binmode ENC;
	print ENC $output;
	exit;
}

One thing that was a gotcha for me was writing the file, I forgot to tell perl to write it binary, since the encryption is all binary, we must write it so. We now have a file called “message” if you try to edit the file, it’s all gobble gook. Tony can now send you this message, and only you can read it, but how?

if ($ARGV[0] eq "--readmessage") {
	$key = $ARGV[1];
	$message = $ARGV[2];
 
	open FILE, "$message" or die $!; 
	binmode FILE;
	my ($buf, $data, $n);
	while (($n = read FILE, $data, 4) != 0) {
		$buf .= $data;
	} 
	close(FILE); 
	$akey = new Crypt::RSA::Key::Private->read(Filename => "$key");
	my $output = $rsa->decrypt(Cyphertext => "$buf",Key => $akey)|| die $rsa->errstr();
	print $output ."\n";
 
	exit;
}

So now we read the file “message” in using our private key, since Tony encrypted the message with our public key, we can now read it with the private key…. So how does this work?

John-Hasss-Mac-Pro:~ john$ ./keylock.pl --makekeys
John-Hasss-Mac-Pro:~ john$ ./keylock.pl --createmessage keylock.public "007 would be so jealous"
John-Hasss-Mac-Pro:~ john$ cat message 
?????u?????מ?n???-@?l?Q??)??~}3?"h2??=,??3??݁/m?s?[?M)?F?}?|ߩJ????k;?7??i???D?\w;?U??ʬ??y7dz??&?]pXv?-???E@"X?E?HH0?(S?"?`?,?R??7???ϳ
  ??Ցh???촇??K??Rs??uxs;??r^΀?G?????5??W??Y???4Y??_~?(YͷӍr\űeĥr??John-Hasss-Mac-Pro:~ john$ 
John-Hasss-Mac-Pro:~ john$ 
John-Hasss-Mac-Pro:~ john$ ./keylock.pl --readmessage keylock.private message
007 would be so jealous
John-Hasss-Mac-Pro:~ john$

Welcome to the magic of public private key encryption!

Have fun!

keylock.zip (27)

2 comments

  1. Matt says:

    John, sometime you are going to have to explain this public/private key stuff to me in plain English. Don and I had a brief conversation about it today, and he actually mentioned your name along with your term “Oogah-boogah”…

  2. John says:

    Funny my life goal, to be mentioned right along with ooga-booga. Keys are fairly easy to understand, give everyone your public key, they encrypt using that, then only you can decrypt using your private key.

Leave a comment

Get Adobe Flash playerPlugin by wpburn.com wordpress themes