Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Math::PARI and Crypt::RSA

by shug94 (Sexton)
on Oct 26, 2009 at 10:36 UTC ( #803235=perlquestion: print w/replies, xml ) Need Help??
shug94 has asked for the wisdom of the Perl Monks concerning the following question:

I have been playing with Crypt::RSA for a while, and I am having some problems getting it to work properly. I am trying to use an external key.
I am generating the private key and writing it to file using the following code:
#!/usr/bin/perl use strict; use Crypt::RSA; print "RSA tester\n"; my $infoToSign = "blah"; my $privateKey = 5; my $rsa = new Crypt::RSA; my ($public, $private) = $rsa->keygen ( Identity => 'RBSClient', Size => 1024, Password => 'passw0rd', Verbosity => 1, ) or die $rsa->errstr(); $private->write( Filename => 'working.private'); $public->write( Filename => 'working.public');
This produces the following private key file (I have reduced the length of the streams of numbers for brevity):
$VAR1 = bless( { 'Version' => '1.99', 'Checked' => 0, 'Identity' => 'RBSClient', 'private_encrypted' => bless( { '_phi' => 'Blowfish d +u2N7P83ABQ cFDt9/0y7IQ 53616c7465645f5f...', '_n' => 'Blowfish Mqm +rxVsrrHbWw RNNCruuuw 53616c7465645f5f658f18eec5ea...', '_q' => 'Blowfish qC0 +F63YxDS8KW LUGDfCMcg 53616c7465645f5f...', '_p' => 'Blowfish sNm +IMm9AuxeF8 uhD/JHszA 53616c7465645f5f...', '_dp' => 'Blowfish p2 +z6NZBV1grw lhJye/R4sw 53616c7465645f5f...', '_u' => 'Blowfish Huh +TLQDF0TEzo Ln7exXdiw 53616c7465645f5f...', '_dq' => 'Blowfish Yf +/aS7U0nI1U GYvagT4J3A 53616c7465645f5f...', '_d' => 'Blowfish SXE +nuMNvxaF2y JFTalOnbQ 53616c7465645f5f...', '_e' => 'Blowfish EzK +9HcfPA2zj4 wouO9lMww 53616c7465645f5f...' }, 'Tie::EncryptedHash' + ), 'Cipher' => 'Blowfish' }, 'Crypt::RSA::Key::Private' );
I then try to create a private key object to sign with, loading from file in the following way:
#!/perl/bin/perl package RBS_PrivateKeyWrapper; use strict; sub getPrivateKey() { my $privateKeyFilename = '/var/www/html/certificates/working.private +'; my $privateKey = getPrivateKeyPerlRSAEncoded($privateKeyFilename); return $privateKey; } sub getPrivateKeyPerlRSAEncoded($) { my ($privateKeyFilename) = @_; my $privateKey = new Crypt::RSA::Key::Private( Filename => $privateKeyFilename ); return $privateKey; } 1;
Up until this point I know that it works fine, because if I comment out the next step there is no problem. However I do actually need to sign the data. I try to sign the data as shown below:
#!/perl/bin/perl use Crypt::RSA; use RBS_PrivateKeyWrapper; my $privateKey = RBS_PrivateKeyWrapper::getPrivateKey(); my $signature = getSignatures($privateKey, $binarySecurityToken, $time +stamp, $content); print $signature; #### sub getSignature($$) { my ($privateKey, $infoToSign) = @_; my $rsa = new Crypt::RSA; my $signatureValue = $rsa->sign( Message => $infoToSign, Key => $privateKey,); return $signatureValue; }
This seems like it should work, but it does not. The errors I get seem to be associated with Crypt::RSA and Math::PARI not playing well together. I am getting the following error (which I do not get if I do not try to sign the data):
[Mon Oct 26 21:15:33 2009] [error] [client] PARI: *** +log is not meromorphic at 0. at /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread +-multi/Mat h/ line 994.
I didn't know what meromorphic meant, so I looked it up and it seems to be saying that you cannot get the logarithm of 0, as there could be infinite values. I think this is due to line 77 of, which is as follows:
sub bitsize ($) { return pari2num(floor(Math::Pari::log(shift)/Matth::Pari::log(2)) + +1); }
So clearly a value of 0 is being passed into this function, and it is not happy with it. Also, the actual line of that is failing is 994, the '&$1;' line listed below (this seems to be some kind of function loading method, but I am unsure):
sub AUTOLOAD { $AUTOLOAD =~ /^(?:Math::Pari::)?(.*)/; # warn "Autoloading $1...\n"; # exit 4 if $1 eq 'loadPari'; my $cv = loadPari($1); # goto &$cv; # goto &$AUTOLOAD; # &$cv; &$1; # &$AUTOLOAD; }
As always, any help would be appreciated. I have Pari and GP installed on my machine, at the latest stable version. I am not sure why this is not working. Has anyone else got any experience with using Crypt::RSA? Do you have any ideas on how to load keys from external files and use them? Or any other ways to do this?

Hopefully someone can help,


Replies are listed 'Best First'.
Re: Math::PARI and Crypt::RSA
by Anonymous Monk on Oct 26, 2009 at 11:43 UTC
    It happens when password is missing
    #!/usr/bin/perl -- use strict; use warnings; use File::Spec; use Crypt::RSA; Main(@ARGV); exit(0); sub Main { my $thisf = File::Spec->rel2abs(__FILE__); my $fprivate = "$thisf-testkey.private"; my $fpublic = "$thisf-testkey.public"; print "RSA tester ($thisf)\n"; my $infoToSign = "blah"; my $privateKey = 5; my $rsa = new Crypt::RSA; my ( $public, $private ); if ( -e $fpublic and -e $fprivate ) { print "reading $fprivate\n"; $private = new Crypt::RSA::Key::Private( Filename => $fprivate, @_ ? ( Password => 'password' ) : (), # NOTICE ARGV ); } else { print "generating keys\n"; ( $public, $private ) = $rsa->keygen( Identity => 'RBSClient', Size => 1024, Password => 'password', ) or die $rsa->errstr(); $private->write( Filename => $fprivate ); $public->write( Filename => $fpublic ); } print "signing $infoToSign\n"; my $signature = $rsa->sign( Message => $infoToSign, Key => $private, Armour => 1, ) || die $rsa->errstr(); print "$signature \n"; } __END__ $ perl RSA tester (~/tmp/ generating keys signing blah PARI: *** negative exponent in glog. at ~/lib/Crypt/RSA/DataFormat line 77. $ $ perl RSA tester (~/tmp/ reading ~/tmp/ signing blah PARI: *** negative exponent in glog. at ~/lib/MSWin32-x86-multi-th +read/Math/ line 994. $ perl asdf RSA tester (~/tmp/ reading ~/tmp/ signing blah -----BEGIN RSA SIGNATURE----- Version: 1.99 Scheme: Crypt::RSA::SS::PSS OQAxMjgAU2lnbmF0dXJlU7UxMIX136kFP+o3o7Pf5orq6jeCJWI7qG/uRqCwFgAoUc/LUR +9jsmBM B1Wl6krjm/98qTYUNgNNWgApxsfTFu23jL3LBbrKH50fNql9YuElxYdER0kyFJgX61xmrg +ivfbm1 Vim4LMMoCSJq2qyhCOEcA1q3zg/p+6U5/Zg8Lbg= =ufjanyZlBik+n/N3tWigfw== -----END RSA SIGNATURE----- $
      This was incredibly helpful and fixed all of my problems. Thankyou very much!
Re: Math::PARI and Crypt::RSA
by proceng (Scribe) on Oct 26, 2009 at 20:04 UTC
    Was this a typo?

    my $signature = getSignatures($privateKey, $binarySecurityToken, $time ++stamp, $content); #==here it's getSignatures print $signature; #### sub getSignature($$) #==here it's <b>getSignature</b> {
    In case it wasn't clear what I meant, the sub is defined as "getSignature($$)", while you called it as "getSignatures" (note the missing s at the end of the function name.
    Otherwise, the previous response should get you there :-)
      No, not a typo, I just didn't include a getSignatures function in my question which calls getSignature a bunch of times based on what inputs we have.

      Sorry for not being clear

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://803235]
Approved by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2018-06-25 12:55 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.