Whats the best method to encrypt a big data file?

Discussion in 'Perl Misc' started by thedifferenZ, Oct 1, 2006.

  1. thedifferenZ

    thedifferenZ Guest

    Hi group,

    I am trying to encrypt/decrypt a data file using asymmetic keys. I
    tried using Crypt:RSA (Blowfish)

    The source file is just 500kb, and its taking more than 20 mins to
    encrypt it. Is there any way to encrypt a file with out taking whole
    file content to memory?

    ========================================================
    use strict;
    use Crypt::RSA;
    my $rsa = new Crypt::RSA;
    my $public = new Crypt::RSA::Key::public(Filename => 'public.txt');
    my $private = new Crypt::RSA::Key::private(Filename => 'private.txt');

    open(DAT,"data.mpg");
    my $message;
    while(<DAT>){
    $message.=$_;
    }
    close(DAT);
    my $cyphertext = $rsa->encrypt (
    Message => $message,
    Key => $public,
    Armour => 1,
    ) || die $rsa->errstr();

    open(WRT,">encrypted.txt");
    print WRT $cyphertext;
    close(WRT);
    ========================================================

    thanks
     
    thedifferenZ, Oct 1, 2006
    #1
    1. Advertising

  2. thedifferenZ

    Dr.Ruud Guest

    thedifferenZ schreef:

    > my $message;
    > while(<DAT>){
    > $message.=$_;
    > }



    Alternative:

    my $message = do { local $/; <DAT> } ;

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Oct 1, 2006
    #2
    1. Advertising

  3. thedifferenZ wrote:
    > Hi group,
    >
    > I am trying to encrypt/decrypt a data file using asymmetic keys. I
    > tried using Crypt:RSA (Blowfish)


    Am I missing something? What is the connection between Crypt::RSA and
    Blowfish. Blowfish AFAIK is a symetric cypher.

    > The source file is just 500kb, and its taking more than 20 mins to
    > encrypt it. Is there any way to encrypt a file with out taking whole
    > file content to memory?


    Most symetric cypher implementations support this mode of operation.
    Asymmetic algorithms are not usually used to encrypt bulk data because,
    as you observe, they are slow so there's little incentive to support
    this mode.

    Perhaps you should use Blowfish to encrypt the bulk data with a random
    key, and RSA to encrypt the key.

    Or you could just use PGP that combines symetric and asymetric.
     
    Brian McCauley, Oct 1, 2006
    #3
  4. thedifferenZ wrote:
    > Hi group,
    >
    > I am trying to encrypt/decrypt a data file using asymmetic keys. I
    > tried using Crypt:RSA (Blowfish)


    RSA is not Blowfish.

    I can't see any reference to Blowfish in your actual code nor in a quick
    glance at the docs for Crypt::RSA.

    >
    > The source file is just 500kb, and its taking more than 20 mins to
    > encrypt it.


    As Brian says, Asymmetric encryption is generally slower than symmetric.

    AFAIK the conventional approach is to generate a random key, use that
    for a fast symmetric encryption of the plaintext, then use asymmetic
    encription to encrypt the random key and include the encrypted random
    key with the main ciphertext.

    The recipient uses their asymmetric key to decode the random key which
    can then be used to decipher the ciphertext.

    > Is there any way to encrypt a file with out taking whole
    > file content to memory?


    Block mode?

    >
    > ========================================================
    > use strict;
    > use Crypt::RSA;
    > my $rsa = new Crypt::RSA;
    > my $public = new Crypt::RSA::Key::public(Filename => 'public.txt');
    > my $private = new Crypt::RSA::Key::private(Filename => 'private.txt');
    >
    > open(DAT,"data.mpg");
    > my $message;
    > while(<DAT>){
    > $message.=$_;
    > }
    > close(DAT);
    > my $cyphertext = $rsa->encrypt (
    > Message => $message,
    > Key => $public,
    > Armour => 1,
    > ) || die $rsa->errstr();
    >
    > open(WRT,">encrypted.txt");
    > print WRT $cyphertext;
    > close(WRT);
    > ========================================================
    >
    > thanks
    >
     
    RedGrittyBrick, Oct 1, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    355
    Larry I Smith
    Jun 21, 2005
  2. Shaguf
    Replies:
    0
    Views:
    563
    Shaguf
    Dec 24, 2008
  3. Shaguf
    Replies:
    0
    Views:
    512
    Shaguf
    Dec 26, 2008
  4. Shaguf
    Replies:
    0
    Views:
    279
    Shaguf
    Dec 26, 2008
  5. Shaguf
    Replies:
    0
    Views:
    260
    Shaguf
    Dec 24, 2008
Loading...

Share This Page