
l4teral
User
Jul 3, 2010, 1:20 AM
Post #1 of 2
(613 views)
|
|
Using Crypt::CBC - keep getting Ciphertext does not begin with a valid header *error
|
Can't Post
|
|
Hey everyone. I have written a script to attempt to push data across a socket, and encrypt the data using the Blowfish encryption method. Here is the raw script that I have so far (work in progress...)
use Crypt::CBC; use IO::Socket; system("cls"); sub encrypter { print "key: "; $key = <STDIN>; chomp($key); my $cipher = Crypt::CBC->new( -key => $key, -cipher => 'Blowfish', -header => 'randomiv', -insecure_legacy_decrypt ); # undef $/; # print "enter message to encrypt: "; # $plaintext = <STDIN>; #chomp($plaintext); open INPUTDATA, "<inputfile.txt"; $plaintext .=<INPUTDATA>; #testing purposes ### print $plaintext; <STDIN>; ### $plaintext = $cipher->encrypt($plaintext); close (INPUTDATA); open MYFILE, ">coded.txt"; print MYFILE $plaintext; close (MYFILE); print "encrypted file written..shh! secret!\n"; #decryption for testing of successful hash. If it didn't hash right, it throws an error in console ############## open MYFILE, "<coded.txt"; $ciphertext .=<MYFILE>; my $cipher2 = Crypt::CBC->new( -key => $key, -cipher => 'Blowfish', -header => 'randomiv' ); $ciphertext = $cipher2->decrypt($ciphertext); ############### #pushes hash out to the socket $MySocket=new IO::Socket::INET->new(PeerPort=>3333,Proto=>'tcp',PeerAddr=>'127.0.0.1'); $MySocket->send("$plaintext\n"); } sub decrypter { $key = $ARGV[1]; print "\n*\n*listening with key as $key\n"; my $sock = new IO::Socket::INET ( LocalHost => '127.0.0.1', LocalPort => '3333', Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Could not create socket: $!\n" unless $sock; my $new_sock = $sock->accept(); while(<$new_sock>) { $ciphertext = $_; chomp($ciphertext); my $cipher2 = Crypt::CBC->new( -key => $key, -cipher => 'Blowfish', -header => 'randomiv' ); $ciphertext = $cipher2->decrypt($ciphertext); # system("cls"); print "\n$ciphertext"; } close($sock); &decrypter; exit(); } #main program if (($ARGV[0] =~ "-e")) { &encrypter } if (($ARGV[0] =~ "-d")) { &decrypter } The problem is, I keep getting Ciphertext does not begin with a valid header for [header] header mode at (script error line) errors randomly, and it seems to happen more frequently, based on how long the data is to be sent. This only says that it's hitting a snag somewhere, and encountering more data to encrypt is like "duh" its going to hit it more often. Can anyone point out anything obvious that is wrong with my code? Feel free to run it. You'll need two instances of it running. one, with a -d [passphrase] option, and the other with a -e option, as it will prompt you for the password to salt the hash with. I'm really stumped on this, and extremely frustrated, because it should work!!!!! Any help would be greatly appreciated
|