perlc1ph3r has asked for the wisdom of the Perl Monks concerning the following question:
I am running up against a wall and hitting it hard. I am needing to encrypt fields and not he entire file for data upload. I then need to use that encrypted field as a key in a system and then when extracted, I need to be able to decypt it. Below are sample lines from a fake text creator to illustrate my quandary."
customerid registered_date first_name last_name address city state zip age primary_email
689-08-2317 1518973472 Sascha Urian 85 Sunbrook Road San Antonio Texas 78250 55 surian0@archive.org
201-25-0510 1515436776 Gilbertine Impy 3136 Thierer Trail Mc Keesport Pennsylvania 15134 49 gimpy1@edublogs.org
616-54-5114 1518645090 Christabella Gunther 4 Autumn Leaf Junction Chicago Illinois 60619 26 cgunther2@nature.com
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. I may be going down the wrong path here and really need a hash process but I have exhausted my working brain cells at this point and need some help. The end format would look like this below.
customerid registered_date first_name last_name address city state zip age primary_email
$#diDse*eES 1518973472 Sascha Urian 85 Sunbrook Road San Antonio Texas 78250 55 weSIDUsae34@#$%%#@@
eSDE#24DSET 1515436776 Gilbertine Impy 3136 Thierer Trail Mc Keesport Pennsylvania 15134 49 sdies#@SERUSEeset
OEDUse#@#$2 1518645090 Christabella Gunther 4 Autumn Leaf Junction Chicago Illinois 60619 26 mvxosef@#w32553S
I have been trying to modify this base code
#!/usr/bin/perl use strict; use Crypt::CBC; #unless (scalar @ARGV == 3) { # #die "Usage: $0 encrypt|decrypt|en|de \$mysecretkey \$file_to_den +crypt"; #} #my $type = shift @ARGV; #my $key = shift @ARGV; #my $file = shift @ARGV; my $type = "de"; my $key = "12345"; my $file = "C:\\perlinputfiletest\\fakedata.txt.encrypt"; die "The first ARGV should be one of de, en, encrypt, decrypt" if ($ty +pe !~ /^(en|de)(crypt)?$/); die "the file $file is not existence" unless (-f $file); 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' ); local $/; open(FH, $file) or die $!; flock(FH, 2); my $data = <FH>; close(FH); my ($save_data, $save_file); if ($type =~ /^en(crypt)?$/) { $save_data = $cipher->encrypt($data); $save_file = $file . '.encrypt'; } else { $save_data = $cipher->decrypt($data); $save_file = $file . '.decrypt'; } open(FH, '>', $save_file) or die $!; print FH $save_data; close(FH); if (-e $save_file) { print "$type file $file to $save_file OK\n"; } else { print "failed without reason\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using Cypher::CBC to encrypt fields in a file - Need Monk Help
by talexb (Chancellor) on Feb 21, 2019 at 19:17 UTC | |
|
Re: Using Cypher::CBC to encrypt fields in a file - Need Monk Help
by bliako (Abbot) on Feb 21, 2019 at 19:50 UTC | |
by talexb (Chancellor) on Feb 21, 2019 at 19:54 UTC | |
by bliako (Abbot) on Feb 21, 2019 at 20:44 UTC |