RSA Encryption in Perl | 3
RSA Encryption in Perl
The Crypt::RC4 Module
Encrypting with the Module
There is a module on CPAN that should work on multiple platforms available at http://search.cpan.org/search?dist=Crypt-RC4. Using the Crypt::RC4 is also more convenient when we want to encrypt something from within a larger Perl program. For example, we may want to encrypt some Web form input, encrypt it, and send it in an email.
#!/usr/bin/perl -w use strict; use Crypt::RC4; use Mail::Sendmail; use CGI qw/:standard/; my $key = '3353bc7'; my $name = param ('name'); my $ccnum = param('cc'); my $ccexp = param('ccexp'); my $message = "$name:$ccnum:$ccexp"; my $encrypted = RC4($key, $message); sendmail((To => 'email@example.com', From => 'firstname.lastname@example.org', Subject => 'New Order', Message => $encrypted, smtp => "my.mail.server")) or die $Mail::Sendmail::error;
In the above example, we are pulling three variables from an HTML form, putting them into a colon delimited string, encrypting it, and then sending it out to a predefined recipient. The receiver, of course, won't be able to read the information without decrypting it first.
#!/usr/bin/perl -w use strict; use Crypt::RC4; my $key = '1c07e37'; open (EMAIL,"email"); my $message = join('',<EMAIL>); my $decrypted = RC4($key, $message); open(FILE,">myfile") || die "$!\n"; print FILE $decrypted; close(FILE);
The program above decrypts the body of the encrypted email and saves it to myfile. Nifty.