http://www.perlmonks.org?node_id=1230322


in reply to Using Cypher::CBC to encrypt fields in a file - Need Monk Help

OK, so it looks like you have some of the framework done. Looking at the Crypt::CBC page on MetaCPAN, you're not encrypting the fields correctly. You need to get each line of the file, split it into fields, encrypt two of the fields, and write the fields out to a new file.

use autodie; # Using this module means open and close errors are # handled nicely. my @affected_fields = qw/0 8/; .. # Figure out the input and the output file names here .. .. open ( my $input_fh, '<', $input_file ); open ( my $output_fh, '>', $output_file ); while ( <$input_file> ) { my @fields = split ( /,/ ); if ( $type =~ /^en(crypt)?$/ ) { foreach ( @affected_fields ) { $fields[ $_ ] = $cipher->encrypt( $fields[ $_ ] ); } } else { # I would prefer to use an elsif here, but # you could catch this during parameter checking. foreach ( @affected_fields ) { $fields[ $_ ] = $cipher->decrypt( $fields[ $_ ] ); } } print $output_fh join (',', @fields ) . "\n"; } close ( $input_fh ); close ( $output_fh );
PS, Always, always, ALWAYS use strict and use warnings.

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.