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

Hi,

I want to pass a variable containing a string to a hidden field in the output html page (assuming I don't want to store the variable elsewhere like writing it to a file or saving it in a cookie). I'm wondering whether there is a perl built in function to encrypt the string so that it's not so easily readable. The reason I need to do this is because the string contains the answer to a puzzle so I want to prevent people from looking at the source code of the html page and discover the answer easily. Presumably I will need a way to decrypt the encrypted string. I don't intend the encryption to be fool-proof - as long as the encrypted string is harder to read. How do I do that?

Please advise me :)

kiat

Replies are listed 'Best First'.
Re: how to encrypt a string...
by Beatnik (Parson) on Dec 22, 2001 at 12:37 UTC
    May I suggest using Crypt::CBC which allows you to format your data so it qualifies for Crypt::* encryption. Caution, not all encryption routines on CPAN do Chain Block Ciphering (but most do). For a builtin function, some might point you to crypt, don't be tempted... stay away from crypt :)

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: how to encrypt a string...
by Zaxo (Archbishop) on Dec 22, 2001 at 11:15 UTC

    The cpan search Crypt gives a long list of methods and modules to choose from. It would probably be as easy to use real crypto as some crackable cypher. Look at RSA and Rijndael.

    After Compline,
    Zaxo

Re: how to encrypt a string...
by kiat (Vicar) on Dec 22, 2001 at 13:42 UTC
    Hi, Thanks for responding. I found a way to encrypt the string. The encryption may be easily guessed or cracked but it does make it harder for people to read what the string is, which is my intention.
    my $Sentence = "Answer to puzzle\n"; print "$Sentence\n"; my $Hex = unpack("H*",$Sentence); print "$Hex\n"; # prints '416e7377657220746f2070757a7a6c650a' my $Sentence2 = pack("H*",$Hex); print "$Sentence2\n"; # prints 'Answer to puzzle'
    cheers, kiat

      How about the old bleach invisible encoding like this:

      my $string = "foobar"; my $enc = unpack 'b*', $string; $enc =~ tr/01/ \011/; print "This is \$enc '$enc'\n"; $enc =~ tr/ \011/01/; print "Now this is \$enc '", (pack 'b*', $enc) , "'\n";

      This is really hard to *see* let alone read - I'd call that a hidden field :-)

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print