I am trying to sign my data using Crypt::OpenSSL::RSA
in perl. This is my code
use Crypt::OpenSSL::RSA;
open $privfh, '>:encoding(UTF-8)', 'private_key';
$rsa = Crypt::OpenSSL::RSA->generate_key(1024);
$key_string=$rsa->get_private_key_string();
print $privfh $key_string;
$rsa_priv = Crypt::OpenSSL::RSA->new_private_key($key_string);
$rsa_pub = $rsa->get_public_key_string();
$plaintext="hello";
$signature = $rsa_priv->sign($plaintext);
print "Signed correctly\n" if ($rsa->verify($plaintext, $signature));
This program is running fine and i am able to sign data correctly. But the issue is that i have to sign a lot of data so i am writing key_string
to a file so that i can use it again and again but the issue is when i try to use it again using following code
use Crypt::OpenSSL::RSA;
open FILE ,'<','private_key';
{
local $/;
$keystring=<FILE>;
}
print "$keystring\n";
$rsa_priv = Crypt::OpenSSL::RSA->new_private_key($key_string);
it is throwing error and not generating $rsa_priv
. The error given by the program is
RSA.xs:178: OpenSSL error: no start line at testperl.pl line 11, <FILE> line 1
.
What should i do so that i am able to signing again and again after generating key for one time only.
You are reading the file into a variable $keystring
but initializing the private key using the variable $key_string
. Change $keystring
to $key_string
.
Perl offers use strict;
to avoid these kind of problems.
Also, if you use use warnings;
, perl will also warn you about accessing unitialized variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With