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

Hi Suppose I have the following file and I want to encrypt the first column and then upload it into the DB. Can someone show me how to complete the follow perl code so that the first column get encrypted and how to decrypt the data? Thanks in advance

FILE: Col1 Col2 Col3 12 A B 15 CD CD 18 CD EF
while(<>){ chomp; ($col1, $col2, $col3)=split(/\s/,$_); $encry_col1=encrypt($col1); $decry_col1=decrypt($col1); print "$encry_col1,$decry_col1, $col1, $col2, $col3\n"; } sub encrypt{ $data=shift; ##how to encrypt this? } sub decrypt{ $data=shift; ##how to decrypt this? }

Replies are listed 'Best First'.
Re: Please Help: encryption of column before uploading to DB
by NetWallah (Canon) on Apr 30, 2013 at 05:24 UTC
    Depends on how much security is required. Please search this site and the Internet for various implementations.

    Here is a simple (but insecure) one - replace both your encrypt and decrypt functions with this:

    sub rot13 { my $text = shift; $text = tr/A-Za-z/N-ZA-Mn-za-m/; return $text; }

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

      Thanks for your efforts...useful and helped
Re: Please Help: encryption of column before uploading to DB
by aitap (Curate) on Apr 30, 2013 at 08:17 UTC

    Can you please elaborate on the purpose of such encryption?

    If you need to store a password, you'd better store a hash (one-way crypto function) and check it than decrypt it back (because using hashes makes it cryptographically impossible to get the original passwords from the database, especially if you salt the passwords). I would recommend Crypt::Eksblowfish::Bcrypt module for hashing passwords.

    If you just need to encrypt arbitrary data, choose a well-known crypto algorythm (Crypt::Blowfish, for example, but there are many others) and hide the key instead of the algorythm: otherwise your algorythm may probably contain some (yet) undiscovered vulnerabilities.

    Edit: corrected typo
      Thanks for your efforts...useful and helped. I used the Crypt::Blowfish, works nicely
Re: Please Help: encryption of column before uploading to DB
by flexvault (Monsignor) on Apr 30, 2013 at 20:30 UTC

    Hello david_lyon,

    I don't know if you need this now, but maybe you or some other monk may need to encrypt something using 'Crypt::OpenSSL::AES'. I added some comments to help understand the process. If anything doesn't make sense, I'll try to explain in detail.

    #!/usr/local/bin/pyrperl -w use strict; use warnings; use Time::HiRes qw( gettimeofday ); my ( %Account ); # Where you keep persistent values my ( $plaintext ); # Text to be en/decrypted my ( $CLOG ); # Log information open ( $CLOG, ">", "./Cipherlogs" ) || Die_Rtn(" ! open-40 $!"); # my $len=length($Account{Cipher}); # Cipher mus +t be 32 bytes in size # print $CLOG "Cipher: |$len|$Account{Cipher}|\n"; # print $CLOG "$value ",length($plaintext),$plaintext,"\n"; # Sh +ows what the text looks like # my $cipher = GetCipher( $Account{Cipher} ); # $Accoun +t{Cipher} is saved seed for the cipher my $seed = ""; my $stime = gettimeofday; for ( 1..32 ) { $seed .= chr(rand(255)); } print length($seed),"\t$seed\n"; my $cipher = GetCipher( $seed ); # Must get the + cipher from the saved $seed print $CLOG " Loop Time Size of text Total calls\n"; + # If you want to verify it's doing work! my ( $no, $value, $total ) = ( 1, 0, 0 ); if ( $cipher ) { while ( 1 ) { my $plaintext = chr($value) x $no; # this is th +e text to be encrypted my $encrypted = Encrypt( $cipher, $plaintext ); my $decrypted = Decrypt( $cipher, $encrypted ); if ( $decrypted ne $plaintext ) { print "NoGood! |$decrypted|$plaintext|\n"; } $value++; $total++; if ( $value > 255 ) { $stime = gettimeofday - $stime; print $CLOG "$stime \t$no \t\t$total\n"; # + If you want to verify it's doing work! $value = 0; $no++; if ( $no > 2**16 ) { last; } $stime = gettimeofday; } } } exit; # Optional ## my $cipher = GetCipher( $key ); sub GetCipher { require Crypt::OpenSSL::AES; my $key = shift; if ( length($key) != 32 ) { return 0; } my $cipher = new Crypt::OpenSSL::AES($key); return $cipher; } ## my $encrypt = Encrypt( $cipher, $data ); sub Encrypt { require Crypt::OpenSSL::AES; my ( $cipher, $data ) = @_; my $encrypted = ""; my $len = length($data); $data = pack("N",$len) . $data; $len = length($data) % 16; if ( $len ) { $data .= "\0" x ( 16 - $len ); } while ( $data ) { $encrypted .= $cipher->encrypt(substr($data,0,16,"") ); } return $encrypted; } ## my $decrypt = Decrypt( $cipher, $encrypted ); sub Decrypt { require Crypt::OpenSSL::AES; my ( $cipher, $encrypted ) = @_; my $decrypted = ""; while ( $encrypted ) { $decrypted .= $cipher->decrypt(substr($encrypted,0,16,"") ); } my $len = unpack("N", substr($decrypted,0,4,"") ); return substr($decrypted,0,$len); } 1; __END__

    I started to respond earlier, but was called away. The value of this technique is that all character from chr(0) through chr(255) can be en/decrypted. The bigger the record the longer it will take to en/decrypted. Hope this helps.

    Good Luck ... Ed

    "Well done is better than well said." - Benjamin Franklin

      Thanks Ed...This looks useful and I will make good use of it...Thanks Again!