Encrypting Text with RC4 - Page 12
September 24, 2001
Encryption and security are becoming commonplace and more
important these days. For example, sending your customer's credit
card in a clear text e-mail is a big no-no. The RC4 encryption
algorithm was invented by RSA and has been widely used for a
number of years and is still secure enough for most uses. The
Encrypt::RC4 module encrypts a text string with the
password that is passed in with the function call. The
RC4 function performs both the encryption and
decryption routines. When encrypting a string, the first
parameter is the password that will be used to encrypt the text.
When decrypting an encrypted string, the first parameter is the
password that will be used to decrypt the text. The second
parameter is the text to encrypt. When running the function to
decrypt text, the second parameter is the encrypted text. But
enough chatter, let's look at some code that encrypts and
decrypts a message.
use Crypt::RC4;
my $encrypted = RC4( $password, $plaintext );
$plaintext = RC4( $password, $encrypted );
The example above encrypts the text contained in
$plaintext using the password in
$password and assigns the encrypted results to
$encrypted. The next line decrypts the encrypted
text, given a valid password, and assigns the decrypted text back
to $plaintext. The example is a round-trip
encryption/decryption routine in two lines of code. Not bad. It
shows how simple it is to use, but it's not a practical example,
so read on.
This more practical example is executed from the command-line and
prompts the user for a password and the file that will be
encrypted. It appends the encrypted file with a .rc4
extension.
#!/usr/bin/perl -w
use Crypt::RC4;
print "Please enter a password: ";
chomp(my $password = <STDIN>);
print "Please specify the file to encrypt: ";
chomp(my $file = <STDIN>);
open(FILE,$file) || die "Cannot open $file for read\n";
my $plaintext = join('',<FILE>);
$encrypted = RC4( $password, $plaintext );
open(FILE2,">$file.rc4") || die "Cannot open $file.rc4 for write\n";
print FILE2 $encrypted;
You can use this script to encrypt a file on your system. Of
course, this example is only half useful. You'll probably need to
decrypt the text at some point, so below is an example that will
decrypt an encrypted file.
#!/usr/bin/perl -w
use Crypt::RC4;
print "Please enter the password: ";
chomp(my $password = <STDIN>);
print "Please specify encrypted file: ";
chomp(my $file = <STDIN>);
open(FILE,$file) || die "Cannot open $file for read\n";
my $encrypted = join('',<FILE>);
my $plaintext = RC4( $password, $encrypted );
open(FILE2,">$file.txt") || die "Cannot open $file.txt for write\n";
print FILE2 $plaintext;
The first and second examples are very similar, but the second
decrypts an encrypted file. You can use these scripts anytime you
need to encrypt sensitive information.
Formatting Text
Do you have to format e-mails, bills, records, HTML pages, and
other unstructured texts into a formatted layout? Perhaps you
need to spiff up that marketing e-mail so that it's centered.
Whatever the requirement, Text::Format is sure to
please. Let Perl take on the work you've been doing by hand. This
module does a good job of turning messy text into a
professionally looking format. And it's easy to use. You can get
a listing of the available routines by typing perldoc
Text::Format on the command-line (after you've installed
it from CPAN).
The sample program below sets the width of the text to 65
characters, sets the left margin to two characters, turns on
justification, adds an extra space after periods, and sets the
indent to 2 characters.
#!/usr/bin/perl -w
use Text::Format;
my $text = new Text::Format;
$text->columns(65);
$text->leftMargin(2);
$text->justify(1);
$text->extraSpace(1);
$text->firstIndent(2);
open(FILE,"message.txt");
my @message = <FILE>;
$text->text(\@message);
print $text->format();
The format() function is capable of cleaning up
badly formatted text and turning it into a well formatted
paragraph. To show what it can do, I've pasted the results of a
message I passed the script above.
I am writing to you regarding the free clue that you sold to
me last week. I am sorry to report that I have not received
any insight into my problems and would like to know if your
product has a return policy or can be brought in for warrantee
service.
Not bad. Hopefully this means a reduction in the time it takes to
fix text by hand.
Processing Text with Perl Modules - Page 11
Weaving Magic With Regular Expressions
Processing URLs - Page 13
|