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.


In reply to Re: Encryption Question by blackmateria
in thread Encryption Question by Purdy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.