PERL encrypt decrypt string with shared key

I want to store an encrypted string in a cookie.
I will keep a key stored in a config file or database
I want to be able to decrypt the string after reading the cookie
I want to use the stored key to decrypt back to readable string

I would like to use pure perl or lightweight module or use a module that is already called in my script.
I already use this for something else:
Crypt::PasswdMD5 qw(unix_md5_crypt);  

any suggestions?
LVL 1
Bob-VillaAsked:
Who is Participating?
 
TintinConnect With a Mentor Commented:
I wrote a Perl/CGI application that has an admin page that you login and it sets an encrypted cookie.

I use Crypt::Tea (Tiny Encryption Algorithms), as it is very light weight as is written in pure Perl.

To encrypt, I do:

        use Crypt::Tea;
        my $user = 'username';
        my $password = 'password';
        my $key = 'somekey';

        $user = encrypt($user,$key);
        $password = encrypt($password,$key);

        # Turn it into a usable format for cookies
        $user = unpack("H*",$user);
        $password = unpack("H*",$password);

It's a similar process for decrypting.


0
 
Adam314Commented:
The Crypt::PasswdMD5 is a one-way crypt - meaning you can't get the original back.

I'm not sure if either of these are pure perl, but take a look:
http://search.cpan.org/~dparis/Crypt-IDEA-1.08/IDEA.pod
http://search.cpan.org/~dparis/Crypt-Blowfish-2.10/Blowfish.pm
0
 
Bob-VillaAuthor Commented:
This is what I was looking for. Should be noted that other modules listed will work but this is the smallest.
Full Usage:

use Crypt::Tea;
        my $user = 'username';
        my $password = 'password';
        my $key = 'somekey';

        $user = encrypt($user,$key);
        $password = encrypt($password,$key);

        # Turn it into a usable format for cookies
        $user = unpack("H*",$user);
        $password = unpack("H*",$password);

        # Get from cookie and pack
        $user = pack("H*",$user);
        $password = pack("H*",$password);

        $user = decrypt($user,$key);
        $password = decrypt($password,$key);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.