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

While trying to get Crypt::RSA to work I started getting some weird errors. I am trying to use a private key to sign some data. The error that I get is below (and I am not sure I understand it):
[Thu Oct 22 15:01:36 2009] [error] [client 10.29.12.38] Can't call met +hod "n" without a package or object reference at /usr/lib/perl5/site_ +perl/5.8.8/Crypt/RSA/ SS/PSS.pm line 41, <PRIVATE_KEY_FILE> line 15., referer: http://192.16 +8.15.2/cgi -bin/RBS_MessageParameterCollectionGUI.pl

The code in question is shown below. Line 41 of PSS.pm is the 'my $k = octet_len ($key->n);' line:
sub sign { my ($self, %params) = @_; my $key = $params{Key}; my $M = $params{Message} || $params{Plaint +ext}; return $self->error("No Key parameter", \$M, \%params) unless $key +; return $self->error("No Message or Plaintext parameter", \$key, \% +params) un less $M; my $k = octet_len ($key->n); my $salt = makerandom_octet (Length => $self->{hlen}); my $em = $self->encode ($M, $salt, $k-1); my $m = os2ip ($em); my $sig = $self->{primitives}->core_sign (Key => $key, Message => +$m); my $S = i2osp ($sig, $k); return ($S, $salt) if wantarray; return $S; }

The code in which I actually use the Crypt::RSA stuff is below:
my $rsa = new Crypt::RSA; my $signatureValue = $rsa->sign( Message => $infoToSign, Key => $privateKey);


If anyone has any idea as to why it is not working, or what might be going wrong, please let me know.

Cheers,
Shug

Replies are listed 'Best First'.
Re: Perl Crypt::RSA problems
by Anonymous Monk on Oct 22, 2009 at 05:20 UTC
    The code in which I actually use the Crypt::RSA stuff is below

    That code does not compile :)

      OK, I'll bite. I am unsure if you are telling me that I need to include my entire chunk of code (cause clearly this is a fragment that won't compile on its own), or if there is some really obvious problem that I am just not seeing.

      I have pulled my code out of the context I am using it, and have written a little bit of practise code to try to get this to work. The practise code still has the same error.

      #!/usr/bin/perl use strict; use Crypt::RSA; print "RSA tester\n"; my $infoToSign = "blah"; my $privateKey = 5; my $rsa = new Crypt::RSA; my $signatureValue = $rsa->sign( Message => $infoToSign, Key => $privateKey,);

      I still get the same problem. I realise that my key in this example is not legitimate, but it should still work, right?

      I might try generating a key using Crypt::RSA too.

        Crypt::RSA says the key argument is a "Private key of the sender, a Crypt::RSA::Key::Private(3) or compatible object." On the other hand, you're passing an unblessed scalar, which has no methods, rather than an object with the API Crypt::RSA expects.

        You'll probably want to use something like
        $private = [doc://Crypt::RSA::Key::Private]->new(filename=>$private_key_file)
        to load the private key, and then pass that object along to sign().