in reply to Encryption/Decryption

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;

Replies are listed 'Best First'.
Re: Re: Encryption/Decryption
by Anonymous Monk on Jan 08, 2004 at 01:15 UTC
    $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.

        Let's take Crypt::RC5 as an example. It's a block cipher, so it can't be used safely without a mode like CBC (the "default" mode, ECB, leaks a lot of information and is very vulnerable to block-shuffling replay attacks). There's a nice module, Crypt::CBC, which adds CBC support to any compatible block cipher, but Crypt::RC5 is not compatible (it doesn't define the blocksize and keysize methods, and its constructor has an extra required parameter). So although Crypt::RC5 does appear to be a correct implementation of RC5, it's basically useless because you need to code the CBC padding and chaining stuff yourself if you want to use it securely. Mr. Kincaid refuses to listen to this type of criticism, preferring instead to argue that it's not his job to make sure people write secure code with his modules.

        Crypt::RC5 appears to have made a half-hearted attempt at supporting CBC mode. There's a decrypt_iv function which seems to do CBC mode decryption, but it's undocumented, doesn't handle padding, and there's no corresponding encrypt_iv.