shivam99aa has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to sign my message with my SSH private key which is in ~/.ssh/id_rsa. But on using Crypt::RSA for signing i keep getting error. Code used by me is

use strict; use warnings; use Crypt::RSA; my $rsa = new Crypt::RSA ( ES => 'PKCS1v15' ); my $pkey = new Crypt::RSA::Key::Private ( Filename => '/home/abc/.ssh +/id_rsa' ); print $pkey;

The error is of this type

Number found where operator expected at (eval 21) line 6, near "ZapzlA +9gZ8 23" (Missing semicolon on previous line?) Bareword found where operator expected at (eval 21) line 6, near "23mq +j2RsKOF0Lal7YbjYFkDE7YrLPktf0FNNYvn6BjKZDlyQfpWnk8dP3crjoown" (Missing operator before mqj2RsKOF0Lal7YbjYFkDE7YrLPktf0FNNYvn6BjK +ZDlyQfpWnk8dP3crjoown?) Number found where operator expected at (eval 21) line 9, near "QUgHQA +J4R 34" (Missing semicolon on previous line?) Bareword found where operator expected at (eval 21) line 9, near "34Cs +y0lcx" (Missing operator before Csy0lcx?) Having no space between pattern and following word is deprecated at (e +val 21) line 10.

there are more messages like the one above and then at last line i get

Can't use an undefined value as a HASH reference at /usr/local/share/perl/5.14.2/Crypt/RSA/Key/Private.pm line 217.

I also want to mention here that i also tried using Crpyt::RSA::Key::Private::SSH but i am not able to get the syntax correct, thus not able to understand if this is solution or not. Not much is given on cpan on how to use it.

Replies are listed 'Best First'.
Re: Using Crypt::RSA for data signing in perl
by Mr. Muskrat (Canon) on Dec 04, 2014 at 19:30 UTC

    shivam99aa is running the code currently list in the OP. Apparently there is a bug or two in Crypt::RSA::Key::Private.

    If you use a private key that doesn't have a passphrase, you get:

    Number found where operator expected at (eval 17) line 3, near ...
            (Missing semicolon on previous line?)
    Bareword found where operator expected at (eval 17) line 3, near ...
            (Missing operator before ...
    Bareword found where operator expected at (eval 17) line 3, near ...
            (Missing operator before ...

    Whether or not the private key has a passphrase, you get:

    Can't use an undefined value as a HASH reference at /opt/perl/lib/site_perl/5.18.2/Crypt/RSA/Key/Private.pm line 217.

    The problems stem from new Crypt::RSA::Key::Private calling its read method that calls its deserialize method which joins the lines into a string that is evaled. The result of the eval overwrites $self which makes it undef.

    So yeah, the OP's code is not at fault here; the module is evaling the private key.

    Update: Crypt::RSA::Key::Private has not seen an update since 2001.

    ## $Id: Private.pm,v 1.15 2001/09/25 14:11:22 vipul Exp $

      Sorry for replying so late on your reply but can you suggest me some alternate way of doing the task

Re: Using Crypt::RSA for data signing in perl
by Anonymous Monk on Dec 04, 2014 at 07:29 UTC
    my $pkey = new Crypt::RSA::Key::Private ( Filename => '/home/abc/.ssh +/id_rsa'
    You forgot closing parens and semicolon.
      Sorry that was a mistake while typing to post the question. In my code i am using closing bracket and semi colon.

        Note that it's actually okay for you to to edit your original question (and you may as well since otherwise we won't have much of a chance at being able to answer it). You should also include the full sequence of error messages you got, or at least the ones at the very beginning, since chances are those are the ones that will show what's actually going wrong.

        Clearly somthing is trying to read a key file or an encrypted file as perl code and failing utterly but without your actual code and your actual first error messages there's no way to tell what's going on.

Re: Using Crypt::RSA for data signing in perl
by Anonymous Monk on Dec 04, 2014 at 15:11 UTC

    As stated, you need to post (in code-brackets ...) the entire actual program that you are trying to run, and a sample of the actual error messages.   The messages that you have posted are clearly syntactical ... missing closing-quote and so on.   But they clearly can’t relate to the snippet of code that you have shown.   Very carefully re-read the documentation for this module and, at first, just try to re-create the examples shown.   The program isn’t working because it isn’t running.   Unil you clean-up the most basic errors, any other messages you receive are merely bilge.