-->

Data signatures using Perl

2020-07-22 10:39发布

问题:

I have been this project which involves sending of signed data.

I have been provided with a .pem file and a server certificate which I am supposed to import to another server from which I should sign the data using these files and then send over the signed data to another server for verification adn processing.

My Questions are:

  • How do I import these two files into my server?
  • How do I use perl to use the files to sign the data?

To note, I am to import into a linux (Suse).

This is what I have done so far I have added the .pem file to /etc/ssl I am using the below code to read from the .pem file then sign the data

my $rsa = new Crypt::RSA;
my $file = 'path to file';
my $key = new Crypt::RSA::Key::Private(  Filename => $file , Password => "*****");
my $signature = $rsa->sign(Message => $data, Key => $key, Armour => 0);

I get errors when i try to run the script as below Bareword found where operator expected at (eval 30) line 4, near "/S2YUeaABL1sO3rk4HhjyDSMIJoc91n.....

I have noted that the contents of the .pem file has slashes within the file which is causing these problem. see sample

-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCMdoWd+LEk22TMaEihLqwl8tJtuFzndJ8dTGrLw6twjfAeV0sg
PsDQbVu5/S2YUeaABL1sO3rk4HhjyDSMIJoc91nfE3kYueRxEA9eL5JlxPDg2Z3s

回答1:

  1. Read Import Certificates and Private Key with Copy and Paste

  2. RSA sign in Perl (untested code):

    use Crypt::OpenSSL::RSA;
    use File::Slurp;
    
    my $keystring = read_file('private_key.pem');
    my $private_key = Crypt::OpenSSL::RSA->new_private_key($keystring);
    $private_key->use_md5_hash(); # use_sha1_hash is the default
    my $signature = $private_key->sign($plaintext);
    

Good luck!



回答2:

Thanks a lot it is now working. I used your example with slight modification as below

use File::Slurp   qw(read_file);
use MIME::Base64  qw(encode_base64);    
require Crypt::OpenSSL::RSA;
my $keystring = read_file( 'path to file name' );
my $privatekey = Crypt::OpenSSL::RSA->new_private_key($keystring);
$privatekey->use_pkcs1_padding();
my $signature = $privatekey->sign($datatosign);
print encode_base64($signature, '');