Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Encrypt Binary Data

by enoch (Chaplain)
on Jul 26, 2002 at 18:48 UTC ( #185625=perlquestion: print w/replies, xml ) Need Help??
enoch has asked for the wisdom of the Perl Monks concerning the following question:

I have been hunting around looking for a module that will encrypt binary data. There are tons of modules that encrypt plaintext, but I could not find one that would encrypt a binary file (say a MS Word document or a PDF file).

What I would like is a module that could encrypt binary data without the use of an external program. (A pure Perl implementation would be nice, but I'll take what I can get.) Does anyone know of such a module where I could use syntax similar to:
use Imaginary::Crypt; $cipher = Imaginary::Crypt->new($key); # $fileName is the path to the file to encrypt # and $newFileName is where the now encrypted # file would reside $cipher->encrypt($fileName, $newFileName);


Replies are listed 'Best First'.
Re: Encrypt Binary Data
by ichimunki (Priest) on Jul 26, 2002 at 18:56 UTC
    What's wrong with the modules you listed? Have you tried them with binary data? Where the PODs for those say "$plaintext" they don't mean plain text in the sense of ASCII, they mean "plaintext" in the context of cryptography-- which is to say, "the unencrypted data".... as opposed to the "ciphertext", which is the encrypted data.
Re: Encrypt Binary Data
by cephas (Pilgrim) on Jul 26, 2002 at 19:26 UTC
    Most any encryption routine should handle any type of incoming data. As already stated plaintext refers to the unencrypted data, not text in the sense of ascii. Also, as a side note, you have Crypt::CBC listed in your set of modules.... CBC is cipher block chaining, which is NOT an encryption mechanism, but a way of making blocks of data dependent on each other to avoid replay attacks.

    I suppose more explanation might be in order here... We'll use Rijndael as our example as it is not the new Advanced Encryption Standard (AES).

    AES is a symmetric key algorithm, this means that the same key encrypts and decrypts (as opposed to asymmetric, or public key algorithms where one key encrypts and the other decrypts).
    First you'll need to generate a key of sufficient strength (128 bit should be sufficient in most applications involving symmetric keys, asymmetric algorithms need much larger keys).
    Then you'll need to pick your mode. I highly suggest using CBC (Cipher Block Chaining) mode. What this does, is XOR each plaintext block with the previous encrypted block, thus making each block dependent on the previous block so blocks can't be inserted into the final encrypted data. I would also recommend using an Initialization Vector (IV) which is a randomized piece of data to kick it off with (it will need to be prepended to the final data to have any hopes of getting your data back.)

    As a side note, the Crypt::CBC module has support for many encryption modules and can handle most of the details for you. Anyways, I hope this was at least somewhat clearer than mud and maybe even helpful.

    Let me know if this all needs more explaining or you want more detail on the subject.

Re: Encrypt Binary Data
by Fletch (Chancellor) on Jul 27, 2002 at 01:21 UTC

    Don't take this personally, but if you don't know the meaning of plaintext as is used in crypto contexts you probably don't know enough to use the modules securely. Some reading is most likely in order; Schneier is a good place to start.

    The former will explain the technical side. The latter goes into why doing crypto and security is hard to get right, even for people that know what they're doing.

Re: Encrypt Binary Data
by gmpassos (Priest) on Jul 27, 2002 at 05:38 UTC
    Well, 2 days ago I posted an answer for a simillar question. Check the node Re: password protect. (with James Bond encrypter/decrypter source). It has the source of a Bit Cypher that I made to play with encrypters. "The code was fully on Perl, can be slow in big files, since an encrypter normally is writed in C".

    "The creativity is the expression of the liberty".

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://185625]
Approved by dws
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2017-07-26 17:48 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (400 votes). Check out past polls.