And this shows how to abstract reading [CST]SV files and keep sane:

#!/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"; ex +it(1) } print "$0 : success, output in '$outfile'.\n";

In reply to Re: Using Cypher::CBC to encrypt fields in a file - Need Monk Help by bliako
in thread Using Cypher::CBC to encrypt fields in a file - Need Monk Help by perlc1ph3r

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.