Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

Which Perl module would you recommend to encrypt hidden values in a web form? I only know CGI::EncryptForm 1.02 but have not used it before.

Replies are listed 'Best First'.
Re: Encrypt web form values
by CountZero (Bishop) on Dec 05, 2008 at 17:07 UTC
    Why would you want to do such a thing? What can be so secret that even the recipient of the web-page should not be trusted to read it?

    If you do not want the data to be known, then just don't send the data. Instead send a cookie and keep the data on your system. The data in the cookie (which can be as simple as a UUID) will be the key to your cookie-vault where the webserver can retrieve it.

    And if you want to avoid eavesdroppers, use a secure protocol such as HTTPS, so third parties cannot even intercept the cookie's content and use it in a replay-attack.

    Oh yes, and of course use only session cookies and expire them in any case after a short while of no connections.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Thanks, you got me thinking.

      I need to encrypt certain values so that they can't be modified by the recipient of the web-page (such as downloading the page and modifying the hidden values). These values will be used when the page is submitted for server-side processing.

      Should I be looking at CGI::Session to store the data? Can the recipient of a web-page manipulate these data for unintended purposes? In order words, can web-form values stored using CGI::Session be reliably used?

        Hi,

        Yes, you should definitely look into CGI::Session. No the user cannot modify it, if you're not allowing it from the server side code. You can store almost whatever you want, and yes it can be reliably used. For storage you have also several options, file, db, cache, etc.

        Regards,

        fmerges at irc.freenode.net
Re: Encrypt web form values
by zentara (Cardinal) on Dec 05, 2008 at 16:45 UTC
    Crypt::RC4 is something fast and encrypted strings can be base64encoded to be put into a hidden field. You can then decode the hidden field on every cgi run.

    You can sort of hide the key from casual eyes, with some code like:

    my $password = pack('C*', (0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef));
    #!/usr/bin/perl use strict; use warnings; use Crypt::RC4; use MIME::Base64; my $key = "abcdefghijklm"; my $plaintext = "Hello, World!"; my $encrypted = RC4($key, $plaintext); my $encoded = encode_base64($encrypted); my $decoded = decode_base64($encoded); print "$encoded\n"; print "$decoded\n"; my $decrypted = RC4($key, $decoded); print "$decrypted\n";

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      I like your simple solution using Crypt::RC4. Thanks!