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

Data File Encryption

by Jamnet (Scribe)
on Apr 23, 2003 at 06:19 UTC ( [id://252460] : perlquestion . print w/replies, xml ) Need Help??

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

Hi All, I am new to data encryption and so I request you all not to flame me. I wish to encrypt and decrypt a data file and I am searching for a module to do the same. I have checked various modules (CPAN) and they either mention about a encrypting small string or are interface to some encryption programs. Please advice me on which module to use preferably a pre-built binary package as I am using ActivePerl and IndigoPerl.

Replies are listed 'Best First'.
Re: Data File Encryption
by vek (Prior) on Apr 23, 2003 at 06:30 UTC
Re: Data File Encryption
by phydeauxarff (Priest) on Apr 23, 2003 at 13:54 UTC
    Welcome to the world of encryption, we can share our secrets wtih you but then we will have to kill you ;-)

    Seriously, if you are wanting to learn more about encryption I would highly recommend picking up Applied Cryptography: Protocols, Algorithms, and Source Code in C by Bruce Schneier at some point...

    Yes, I know...it's not a perl book but it is an excellent resource on cryptography programming as evidenced by the well used copy I keep borrowing from DrManhattan

    For your specific application, are you wanting to use symmetric encryption...say, to store a file away from prying eyes. Or, do you need something asymmetric like PGP to send an encrypted file...i.e. an e-mail to someone so they can decrypt it?

    If you are wanting symmetric encryption of a file, then Crypt::CBC is a breeze...

    To Encrypt using Blowfish...

    #! /usr/bin/perl use Crypt::CBC; $cipher = Crypt::CBC->new( {'key' => 'my secret key', 'cipher' => 'Blowfish', 'iv' => '$KJh#(}q', 'regenerate_key' => 0, # default true 'padding' => 'space', 'prepend_iv' => 0 }); $cipher->start('encrypting'); open(F,"./plain_file"); open(STDOUT,">crypt_file"); while (read(F,$buffer,1024)) { print $cipher->crypt($buffer); } close STDOUT;
    And to decrypt...
    $cipher->start('decrypting'); open(F,"./crypt_file"); while (read(F,$buffer,1024)) { print $cipher->crypt($buffer); }

    That should get you started

Re: Data File Encryption
by crouchingpenguin (Priest) on Apr 23, 2003 at 11:27 UTC
    Jamnet,

    You can try Data::Serializer to encrypt your data (whether it's simply data or data structures). It uses the Crypt:: modules, Crypt::Blowfish by default.


    cp
    ----
    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
Re: Data File Encryption
by nothingmuch (Priest) on Apr 23, 2003 at 15:02 UTC
    Encryption splits into two main categories:

    Symmetric key encryption
    This type of encryption is a way of processing data with a key, storing it somewhere, and then, using the same key, reversing the process so that the original data is retreived. Algorithms which do this are RC4, DES, Rajendal (eh?) Rijndael, Blowfish, and Towfish. They are usually very easy to implement, and you can, if you need, implement them in pure perl quite easily yourself. Search for these on http://search.cpan.org and see what comes up
    Asymetric key encryption
    This is the heavier grade encryption. It's slower, and more complete. You have one key to encrypt, and one key to decrypt. Security is better, because you don't need a protocol to ensure that the two sides of a correspondence know the symmetric key, rather each knows the other public, or encrypting key, and encrypts data so that only the other side can decrypt it. Modules which take care of this have been mentioned. Generally this is GNUPG, and PGP stuff. The algorithms are usually still not that complicated, but the auxillery operations are - generating huge prime numbers, for example. El Gammal, RSA, and DSA are algorithms which may be of concern to you. the module Crypt::OpenPGP is a pure perl one, but unfortunately requires the non easy build Math::PARI.
    For encrypting files which are stored you probably want to use a symmetric key encryption, and as the key you can use a Digest::MD5 or Digest::SHA1 hash of a passphrase, or even the passphrase itself if it suffices. Public key encryption becomes necessary when you want to pass files between two computers on an unsafe communication. If you are interested in that, perhaps a secure protocol, like https, ssh, or some other is more suitable. Public key encryption has it's use mainly in emails.

    Finally i'd like to note that since your question is clear, non obstrusive, polite, and to the point (and a reasonable point too), there's no reason any community would flame you. Some communities are intolerant to newbies, but those are usually much more specific in their interests. Perlmonks is too general for snobism.

    Good luck!

    Updates: Fixed phonetic memory wordo to the real name of Rijndael, Fixed OpenPGP to Crypt::OpenPGP

    -nuffin
    zz zZ Z Z #!perl
Re: Data File Encryption (with Crypt::OpenPGP?)
by markjugg (Curate) on Apr 23, 2003 at 14:57 UTC

    I'm surprised no one has mentioned Crypt::OpenPGP. It appears to be a full-featured Pure-Perl implementation.

    However, I must admit in my own efforts to start using it recently on FreeBSD, I've run into what appears to be a bug in the software, related to the Crypt::Random module. (I did get it working fairly easily on Linux once before).

    Does anyone have any comments on Crypt::OpenPGP while we're on the topic?

    -mark