Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by talexb (Canon)
on Feb 21, 2019 at 19:17 UTC ( #1230322=note: print w/replies, xml ) Need Help??


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

    I would need to read in the file, encrypt column 1 & 9, then output the file in the same format but with those values having encrypted values.

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1230322]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2019-07-18 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?