#!/usr/bin/perl use strict; use warnings; use Crypt::CBC; use Text::CSV; use Data::Dumper; my $type = "en"; my $key = "12345"; my $file = "data.txt.encrypt"; my $DEBUG = 1; print "type is $type, key is $key, file is $file\n" if $DEBUG; my $cipher = Crypt::CBC->new( -key => $key, -cipher => 'Blowfish' ); my $data = Text::CSV::csv( in => $file, sep_char => ' ', headers => 'auto', keep_headers => \my @headers ); if( ! $data ){ print STDERR "$0 : call to Text::CSV::csv() has failed for input file '$file'.\n"; exit(1) } print Dumper($data) if $DEBUG; my ($outfile, $cipher_coderef); if( $type =~ /^en(crypt)?$/ ){ $cipher_coderef = sub { return $cipher->encrypt($_[0]) }; ($outfile = $file) =~ s/encrypt/decrypt/; } else { $cipher_coderef = sub { return $cipher->decrypt($_[0]) }; ($outfile = $file) =~ s/decrypt/encrypt/; } my @columns_to_encrypt = ('customerid', 'age'); my $rid = 0; foreach my $row (@$data){ $rid++; foreach my $colname (@columns_to_encrypt){ print "$0 : row $rid, column '$colname' : processing ...\n"; $row->{$colname} = $cipher_coderef->($row->{$colname}); } } if( ! Text::CSV_XS::csv( in => $data, sep_char => ' ', headers => \@headers, out => $outfile ) ){ print STDERR "$0 : call to Text::CSV_XS::csv() has failed.\n"; exit(1) } print "$0 : success, output in '$outfile'.\n";