Signing data using Crypt :: OpenSSL :: RSA in perl

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 works fine, and I can correctly sign the data. But the problem is that I need to sign a lot of data, so I write key_string in the file so that I can use it again and again, but the problem is that I try to use it again using the 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 throws an error and does not generate $rsa_priv . Error indicated by the program, RSA.xs:178: OpenSSL error: no start line at testperl.pl line 11, <FILE> line 1 . What should I do so that I can sign up again and again after generating the key only once.

+6
source share
1 answer

You read the file in the $keystring variable, but initialize the private key using the $key_string variable. Change $keystring to $key_string .

Perl suggests use strict; to avoid such problems. Also, if you use use warnings; , perl will also warn you about access to unified variables.

+1
source

Source: https://habr.com/ru/post/980289/


All Articles