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

I need something encrypt text so it can be decrypted only when you know the right code that is used to encrypt it. this needs to be CGI so it can be printed out. Can someone tell me how to - A) Encrypt the text by a code I want to use. B) Decrypt the encrypted text by using the code to encrypt it by. It needs to be secure, but for what I need it for, it doesnt have to be 100% unbrakable (nothing is).

Replies are listed 'Best First'.
Re: Encrypt/Decrypt
by Beatnik (Parson) on Aug 03, 2002 at 06:41 UTC
    Check CPAN for the Crypt:: modules. Crypt::Rijndael is one that pops to mind but I'm sure the other dozen or so routines work equally well.

    Greetz
    Beatnik
    ...Perl is like sex: if you're doing it wrong, there's no fun to it.
Re: Encrypt/Decrypt
by crenz (Priest) on Aug 03, 2002 at 05:01 UTC
    jeffa posted an encryption module a while ago, see Crypt::Rot26. It's also quite fast. ;-)
Re: Encrypt/Decrypt
by derby (Abbot) on Aug 03, 2002 at 11:47 UTC
Re: Encrypt/Decrypt
by zentara (Cardinal) on Aug 03, 2002 at 17:26 UTC
    If you need it for cgi on a remote server, I would suggest
    the Crypt::RC4 module from cpan. It is pure perl so you can
    upload it to your server and use it.
    This also uses MIME::Base64 to make your data printable
    for sending over the net.
    If you need to upload the RC4.pm to your server, you need to
    make a few changes.
    BEGIN{unshift(@INC,'/home/yourusername/public_html/cgi-bin')} use RC4;
    #!/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";
Re: Encrypt/Decrypt
by TheFifthDeuce (Novice) on Aug 03, 2002 at 18:16 UTC
    I have a pretty sweet encrypter/decrypter online that I wrote in Perl. Feel free to check it out and see if this is something that might interest you:

    http://www.trixmaster.com/designs/encrypter.pl

    It has 144-bit key strength, plus I added a little extra trickery to epitomize randomness of outputted ciphertext. If I told ya the code, I would then have to kill you.lol(j/k)

    David
      Congratulations on learning how to make a working link.

      However I still hope that nobody is fooled by your post into trusting an unverified encryption algorithm written by a novice. Particularly not when there are plenty of well-studied and well-analyzed algorithms with freely available (and analyzable) implementations like Crypt::CipherSaber (uses a shared secret), Crypt::DH (agree on a secret over an insecure channel) and Crypt::OpenPGP (public/private key implementation). Each of which is appropriate in a different situation, and all of which have a lot more experience backing them than yours.