PERL encrypt decrypt string with shared key

Posted on 2007-07-27
Medium Priority
Last Modified: 2013-12-25
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?
Question by:Bob-Villa
LVL 39

Expert Comment

ID: 19582531
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:
LVL 48

Accepted Solution

Tintin earned 2000 total points
ID: 19590225
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.


Author Comment

ID: 19591370
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);

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question