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

Encrypting strings with more than 8 bytes.

by Andre_br (Pilgrim)
on Jan 13, 2006 at 00:41 UTC ( [id://522850]=perlquestion: print w/replies, xml ) Need Help??

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

Hello steemed fellow Monks

I´m in need of thy help. I want to encript (and later decrypt) a string with more than 8 words. Crypt::Blowfish and all others seem to accept only 8-bytes-long strings!!! How to overcome this?

use Crypt::Blowfish; my $key = pack("H16", "0123456789ABCDEF"); # min. 8 bytes my $cipher = new Crypt::Blowfish $key; my $ciphertext = $cipher->encrypt("but my string has more than 8 byt +es!!!!"); # SEE NOTES print unpack("H16", $ciphertext), "\n";
Thanks a lot

Replies are listed 'Best First'.
Re: Encrypting strings with more than 8 bytes.
by Aristotle (Chancellor) on Jan 13, 2006 at 01:33 UTC

    Use it indirectly via Crypt::CBC.

    Makeshifts last the longest.

Re: Encrypting strings with more than 8 bytes.
by athomason (Curate) on Jan 13, 2006 at 05:07 UTC
    Crypt::Blowfish encrypts just a block (your 8 bytes) at a time; you'll need another module to handle real data. Splitting your data in 8-byte blocks, encrypting each, and combining is the obvious but insecure way to do this; Cipher Block Chaining is a method for doing it securely. As Aristotle mentions, Crypt::CBC will do the work for you with a wee bit of effort; Crypt::CBCeasy is supposed to make things Just Work in a way like what you want. You should be able to do this (untested):
    use Crypt::CBCeasy; my $key = pack("H16", "0123456789ABCDEF"); # min. 8 bytes my $ciphertext = Blowfish::encipher($key, "my string has lots of bytes +!!!!"); print unpack("H16", $ciphertext), "\n"; my $deciphertext = Blowfish::decipher($key, $ciphertext); print $deciphertext, "\n";
Re: Encrypting strings with more than 8 bytes.
by diotalevi (Canon) on Jan 13, 2006 at 05:43 UTC

    Since you're encrypting credit cards, Crypt::CreditCard would be the right (and easy) way to do this.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      Though Crypt::CreditCard looks like a good approach, the only versions availabe on CPAN are developer releases and the perldoc explicitly states it is not intended for production use at this point.

      Unless you want to display the credit card number back to the user at some point (which does not sound like good practice to me) you may want to look into using an asymmetric encryption method (such as GPG) for credit card numbers. This ensures that the numbers cannot be stolen even if both your database and the public key you use to encrypt the data have been compromised (providing you keep the private key off-site and safe).


      There are ten types of people: those that understand binary and those that don't.

        I ddidn't read the pod closely enough to see the warnings but did notice that it was accepting some kind of keys and I vaguely guessed it was doing something asymetric. Maybe not. It's what Andre should be using: it leaves things safe on the public computer.

        [Added; My language wasn't clear. "It" in "It's what Andre should be using" referred to something assymetric like gpg. I was agreeing with whoever it was that I was responding to.]

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      Since you're encrypting credit cards
      Sometimes I miss things, but where did the OP say anything about credit cards?

      thor

      The only easy day was yesterday

        He or she said that in the chatterbox.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-19 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found