Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Encryption/Decryption

by Anonymous Monk
on Jan 07, 2004 at 18:59 UTC ( [id://319558]=perlquestion: print w/replies, xml ) Need Help??

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

I want to do some encryption/decryption on just a couple of strings. I was looking at the variety of Crypt:: modules on CPAN. I am using Windows XP, what Crypt module would be good to use (specifically a standalone)?

Thanks for the help!

Replies are listed 'Best First'.
Re: Encryption/Decryption
by hardburn (Abbot) on Jan 07, 2004 at 19:22 UTC

    What sort of encryption do you need?

    For public key encryption (where the public key encrypts and the private key decrypts), I like Crypt::OpenPGP (though it has a massive ammount of dependencies, including the annoying-to-install Math::Pari).

    For good-old block cipher encryption, you probably want Crypt::CBC using Crypt::Rijndael as the underlieing block cipher. Unless you have a need for another cipher mode (if you don't know what this means, look it up, or just use CBC).

    For stream ciphers, there is Crypt::RC4. RC4 might have some problems, but there aren't really any better stream ciphers (though most of the time you can drop in a block cipher instead).

    If you're just storing some passwords, you probably only need a digest, not a cipher. Try Digest::SHA1.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Re: Encryption/Decryption
by blue_cowdawg (Monsignor) on Jan 07, 2004 at 19:27 UTC

        what Crypt module would be good to use

    The absolute answer is: "It depends!"

    My personal preference is Crypt::Blowfish in terms of ease of use. If I am interested in using some sort of public key/private key scheme then Crypt::GPG is going to get considered.


    Peter L. Berghold -- Unix Professional
    Peter at Berghold dot Net
       Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.
      Blowfish rocks and you just gotta love the name.....
Re: Encryption/Decryption
by phydeauxarff (Priest) on Jan 07, 2004 at 19:12 UTC
    I have been using crypt::cbc from time to time for a while

    I gave an example in Re: Data File Encryption that should provide you with a good starting point for consideration.

Re: Encryption/Decryption
by zentara (Archbishop) on Jan 07, 2004 at 21:27 UTC
    RC4 is about your easiest encryption. There are alot of others too, RC5, and Rijndael. The biggest problem for beginners is the need to make either the data or key a certain blocksize. Some algorithms don't require this.

    Here are a couple of examples:

    #!/usr/bin/perl use Crypt::RC4; $passphrase = "rumpelstiltskin"; $plaintext= qw(qqq"""""""''''''';;;;;qwweeerrrtttyyyy); $encrypted = RC4( $passphrase, $plaintext ); $decrypt = RC4( $passphrase, $encrypted ); print "$plaintext\n$encrypted\n$decrypt\n";

    or

    #!/usr/bin/perl use Crypt::RC5; $key = 'e726f4a56b3e4f'; $rc5 = new Crypt::RC5($key, '12' ); open (FH,"< /etc/passwd"); while(<FH>){ $file .= $_ } close FH; print "$file\n"; $cipher = $rc5->encrypt($file); $rc5d = new Crypt::RC5($key, '12' ); $plain = $rc5d->decrypt($cipher); print "$plain\n";

    or

    #!/usr/bin/perl use Crypt::Rijndael; $key = chr(0) x 32; #key is all nulls here substr($key, 0, 1) = chr(1); #put 1 chr(1)in key just for fun print "key->$key\n"; $plaintext= "adrqwqqqqqqqqqqqqqqqqqqqqqqwrxcq4gfq3g2q45g2q43g5"; print "plaintext->$plaintext\n"; $plaintext16= get16($plaintext); print "plaintext16->$plaintext16\n"; $cbc = new Crypt::Rijndael $key, Crypt::Rijndael::MODE_CBC; $crypted = $cbc->encrypt($plaintext16); print 'crypted->',"$crypted\n"; #keep encrypted string in different pr +int string #to avoid character corruption of prec +eding string $cbc = new Crypt::Rijndael $key, Crypt::Rijndael::MODE_CBC; $decrypted = $cbc->decrypt($crypted); print "decrypted->$decrypted\n"; #this sub makes all data blocksize of 16 bytes. sub get16 { my $data = shift; print "data=$data\n"; return "\0" x ( 16 - length($data)%16 ) . $data; } exit;
      $cipher = $rc5->encrypt($file); This encrypts in ECB mode. Don't do that. IMAO, don't use any crypto module by Kurt Kincaid.

      If you're on WinXP, maybe you don't have a compiler, so you either need to find a precompiled crypto module (in .ppm format, maybe), or use a pure perl implementation. Those are usually named with _PP or ::Perl on the end. They're slower, but it won't matter much for short strings.

        IMAO, don't use any crypto module by Kurt Kincaid.

        Explain arrogant opinion.

Re: Encryption/Decryption
by b10m (Vicar) on Jan 07, 2004 at 19:12 UTC
Re: Encryption/Decryption
by talexb (Chancellor) on Jan 07, 2004 at 19:12 UTC

    That's a pretty wide open question, but you could try out Digest::MD5 as a start.

    Alex / talexb / Toronto

    Life is short: get busy!

      How would you decrypt a MD5-encrypted string? ;)

      Update: MD5 is one-way hash, and therefore, you aren't able to "decrypt" something you "encrypted" with MD5. This can be useful for passwords (and some operating systems use MD5 for their password file). You make a MD5 hash of the password once and store that output. When, later, a user types in his/her password, you MD5 that string too and compare the results. But since you specifically spoke about decrypting, this type (MD5) doesn't seem valid here. (Thanks to Limbic~Region for pointing out the lacking explanation)

      Update: changed terminology a bit

      --
      b10m

        D'Oh! I looked at the post carefully and registered 'encryption' only. Need more coffee, clearly.

        Alex / talexb / Toronto

        Life is short: get busy!

        How would you decrypt a MD5-encrypted string?

        By being very, very patient.

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        : () { :|:& };:

        Note: All code is untested, unless otherwise stated

        How would you decrypt a MD5-encrypted string? ;)

        Well I work for NSA and with a CRAY and the right hooks you can turn 128bits into the complete works of Shakespere ;)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-26 03:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found