Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Cypto CBC and Blowfish

by Will C (Initiate)
on Apr 08, 2013 at 21:28 UTC ( #1027602=perlquestion: print w/ replies, xml ) Need Help??
Will C has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I am having problems using CBC and Blowfish. I have extensively searched the internet, including PerlMonks, and I cannot figure out what I am doing wrong. Below is the code I am using to encrypt:

use Crypt::CBC; my $cipher = Crypt::CBC->new( -key => 'fjeislqp12345678', -cipher => 'Blowfish'); .. various parameter and file declarations.. $cipher->start('encrypting'); for (my $i=0; $i < @$name; $i++) { $param_name = $name->[$i]; $param_value = $value->[$i]; $lineout = $param_name . "," . $param_value . "," . $filedate +. "," . $utid . "\n"; print LATEST $cipher->crypt($lineout); print ALLFILE $cipher->crypt($lineout); print LAFILE $cipher->crypt($lineout); } print LATEST $cipher->finish(); print ALLFILE $cipher->finish(); print LAFILE $cipher->finish();

All of the above seems to be working OK. However when I tried to decrypt I get the, now infamous, error message: Ciphertext does not begin with a valid header for 'salt' header mode. Below is the code I wrote for decrypting:

#!C:\strawberry\perl\bin\perl # use Crypt::CBC; # my ($line, $outf, $filen); my $cipher = Crypt::CBC->new( -key => 'fjeislqp12345678', -cipher => 'Blowfish'); # get filename if($#ARGV == -1) { print STDERR "$runtime::No data directory specified on the com +mand line\n"; close(STDERR); exit; } $filen = $ARGV[0]; # $outf = "test.txt"; open(OUTFILE, ">>", $outf) or die "Can't open $outf to write: $!\n +"; open(INFILE,$filen) or die "Can't open $filen for reading $!\n"; $cipher->start('decrypting'); print OUTFILE $cipher->crypt($_) while(<INFILE>); # while (<INFILE>) { # $line = $_; # print OUTFILE $cipher->crypt($line); # } print OUTFILE $cipher->finish(); close(INFILE); close(OUTFILE);
I would very much appreciate any assistance with this.

Comment on Cypto CBC and Blowfish
Select or Download Code
Re: Cypto CBC and Blowfish
by Anonymous Monk on Apr 08, 2013 at 21:42 UTC

    ... crypt ...

    I can't offer anything more specific than

    I would use Crypt::CBCeasy

    I would also use binmode

    Where did you learn about $#ARGV == -1?? The perl way to write that is

    if( @ARGV ){ print "have args\n" } else { print "got none\n"; }

      Another poster also suggested using BINMODE, however I have tried it a few different ways (see my second post) and am not getting the results I need. As to $#ARV== -1, I do not recall specifically. I have been writing Perl for about 4 years now and relied heavily on the Perl Black Book and the Perl CookBook back then so my guess is either from one of those or perhaps one of the online tutorials I took. I appreciate your advice though.

Re: Cypto CBC and Blowfish
by Khen1950fx (Canon) on Apr 09, 2013 at 09:31 UTC
    Does this help?
    #!/usr/bin/perl -l BEGIN { eval { require Crypt::Blowfish; }; } use strict; use warnings; use Crypt::CBC; my ( $line, $outf, $filen ); my $cipher = Crypt::CBC->new( -key => 'fjeislqp12345678', -cipher => 'Blowfish' ); $cipher->start('encrypting'); $filen = $ARGV[0]; $outf = "test.txt"; my $ciphertext = $cipher->encrypt($filen); my $plaintext = $cipher->decrypt($ciphertext); if ($@) { print "No data directory specified"; } open INFILE, '<', $filen or die "Can't open $filen for reading $!\n"; binmode INFILE, ":encoding(UTF-8)"; open OUTFILE, '>', $outf or die "Can't open $outf to write: $!"; binmode OUTFILE, ":encoding(UTF-8)"; $cipher->start('decrypting'); $plaintext = $cipher->decrypt($ciphertext); print OUTFILE "Decrypted $plaintext"; $cipher->finish(); close INFILE; close OUTFILE;

      This does not work for what I need as it only seems to encrypt and decrypt $filen and not the contents of the file. Perhaps the error has to do with the way I am writing the multi-line file out. I have tried using BINMODE however the output file is unreadable. Here is what my code does: I read a file and construct parameter name and parameter value pairs. I send these arrays to the code listed below for encryption. I then have a second Perl script which reads the encrypted file and decrypts it back to readable text. What I have tried: No BINMODE and I get the SALT error. Use BINMODE when outputting the encrypted file, use BINMODE when I read in the encrypted file. The result is the SALT error again. Use BINMODE when outputting the encrypted file, use BINMODE on both the input and output files when decrypting and the result is an output file in binary not text. Perhaps I am not understanding what you are trying to convey? Below is the code for encrypting:

      use Crypt::CBC; my $cipher = Crypt::CBC->new( -key => 'fjeislqp12345678', -cipher => 'Blowfish'); # get input arrays my($name, $value) = @_; # open output files my($latest_file,$all_file, $latest_all_file); my($param_name, $param_value,$file_time,$lineout); my($cipher_text); $file_time = get_time_for_file(); $latest_file = "C:\\hc\\logs\\SC\\TO\\Bode_" . $utid . "_latest_" +. $file_time . ".txt"; open(LATEST, ">>",$latest_file) or die "Can't open $latest_file $! +\n"; binmode LATEST, ":encoding(UTF-8)"; # $cipher->start('encrypting'); for (my $i=0; $i < @$name; $i++) { $param_name = $name->[$i]; $param_value = $value->[$i]; $lineout = $param_name . "," . $param_value . "," . $filedate +. "," . $utid . "\n"; print LATEST $cipher->encrypt($lineout); } print LATEST $cipher->finish(); close(LATEST);

      Below is the code to decrypt:

      #!C:\strawberry\perl\bin\perl # BEGIN { eval { require Crypt::Blowfish; }; } use Crypt::CBC; # my ($line, $outf, $filen); my $cipher = Crypt::CBC->new( -key => 'fjeislqp12345678', -cipher => 'Blowfish'); # get filename if($#ARGV == -1) { print STDERR "$runtime::No data directory specified on the com +mand line\n"; close(STDERR); exit; } $filen = $ARGV[0]; # # $outf = $ARGV[0]; $outf = "test.txt"; open(INFILE,$filen) or die "Can't open $filen for reading $!\n"; binmode INFILE, ":encoding(UTF-8)"; open(OUTFILE, ">>", $outf) or die "Can't open $outf to write: $!\n +"; # binmode OUTFILE, ":encoding(UTF-8)"; $cipher->start('decrypting'); # print OUTFILE $cipher->decrypt($_) while(<INFILE>); while (<INFILE>) { $line = $_; print OUTFILE $cipher->decrypt($line); } # print OUTFILE $cipher->finish(); $cipher->finish(); close(INFILE); close(OUTFILE);

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2014-09-20 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (153 votes), past polls