Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Encryption Question

by blackmateria (Chaplain)
on Nov 02, 2001 at 00:27 UTC ( [id://122662]=note: print w/replies, xml ) Need Help??


in reply to Encryption Question

IMO, if you're just sending links to current subscribers you don't need to actually encrypt the link, you just need to add a MAC. The problem with sending plain subscriber ID's in links is that anyone with a subscriber ID, who gets a link from you, can trivially figure out anyone else's link (and maybe forge renewals in their name, etc., depending on the rest of your site design). So just use Digest::MD5 to compute the hash of the subscription ID and a secret (e.g. "open sesame"):
use Digest::MD5 ; my $id = '213455' ; # WARNING: this is the third password any cracker would guess, don't r +eally use it. my $secret = 'open sesame' ; my $mac = Digest::MD5::md5_hex ("${id}${secret}") ;

Then just tack the MAC onto the end of the subscription ID: my $url = "http://www.mysite.com/renew.cgi?id=${id}${mac}" ;

Actually, you could even add the MAC as another CGI parameter, it wouldn't make any difference. The link is shorter this way though (my example gives a URL of http://www.mysite.com/renew.cgi?id=213455da74bbd77b3890eadce45ef45add94c8 -- I don't know if that's short enough or not. If you need it to be shorter, you could use md5_base64 instead of md5_hex, but be careful because standard base64 includes some characters (like "+") that aren't compatible with URL's.) Then just check the MAC when the client surfs in. Presto, nobody can fake a link anymore unless they can read the victim's email. Of course, this is all totally off-base if you're posting these links on a public web page or something. The basic principle would remain the same even in that case: never trust data coming from the client. HTH!

Disclaimer: IANACE (I am not a crypto expert)

Update: Well, I was surprised to learn that "${id}${secret}" does not mean the same thing as "$id$secret," although both would work correctly in my example above. Apparently the ${} form is actually a symref, which should not be used unless you already know why they are bad. :) I had no idea there was a difference between the two forms (actually it was a complete accident that I used the ${} form in my example, normally I stay away from it.) Thanks to crazyinsomniac for picking this one up.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://122662]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-25 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found