in reply to Crypt::OpenSSL:RSA Questions

Superficially, it appears that you made an appropriate choice.   Do you have any in-progress Perl code that you could share with us?   (Or, for that matter, PHP code?)   We’ll be happy to help you with that.   (Remember to use code-tags, if it is large.)

It appears to me that this package has a very object-oriented approach, versus PHP’s decidedly functional one.   PHP, more or less by-tradition, has a very monolithic approach to things, where you call certain functions to load information (into some “magic” but unknown location in space/time ...) so that other functions can subsequently refer to it.   This object-oriented approach is much cleaner, but it does mean that the calling sequence will be different.

The corollary to openssl_get_privatekey(), it seems to me, would be the new_private_key() method, which is an object constructor.   (There is also a new_public_key() method, although it seems to take a string whereas PHP takes a certificate file-name.)

Note – The Perl method-names are somewhat misleading:   you are not, say, creating a “new private key.”   Rather, you are invoking a variation of new() which takes a private-key string.   What you wind-up with, is a Perl object instance.

Having thus successfully created a Perl object-instance which incorporates a key (using any one of the several methods provided), you would then invoke methods of that particular instance to do the various things that you need, such as signing a string or verifying a signed string.

Unfortunately, this module author’s documentation (sic ...) is working against you.   The PerlDoc page is very ... sparse.   C’est la guerre.   Aside from the example, there seems to be no discussion of the actual methods ... merely an enumeration of the methods that exist.   Nevertheless, I think that with a very short test-script, heavily based on such examples as there are, I think that you ought to be able to muddle-through how to use this module.   And, I think that you did find the right one.

Try constructing a minimal test-script, and feel free to post it here for peer-review.

Replies are listed 'Best First'.
Re^2: Crypt::OpenSSL:RSA Questions
by martin87 (Initiate) on Jun 19, 2014 at 22:15 UTC

    You sir deserve something special! I first made a php-script with the help of the example from the webservice-documentation. From this script i got end result which is useful in this case since I don't really have any clue what I'm doing :)

    The result from the PHP-script was:

    Signature: NQXhQ5mk8bNQl1BvRMJoCqp2gpyuywzfs7KRAKIFxFK4ujfJ78hCYVbrieYGgIQCQD7Wp2NZnAeZvWuws2BiP8c+4HMOIwMqIQKOBQvxhpM0q6TsPR0pkOr0IV1kLbkfzKVsXC71rGkEE4pMrIaCxJ1yVxM0cmxKh4vj9nUjYgiSAWzR76YdptHOwG99dGjXfiCTiqD7DGe8qnkW2U/DAo3F+CP+fX9DfgWxUhCr1MWk87ctbJnlxsl2C3+lJSVgpnRVJ3nJtHmWfqF/iflGUIJ4u333B1fIBzMLsY58lKyDRpT0UmCajQ9eGZNy9uKWR9ZnrkajG08tepnvEClboQ==

    This is an encoded base64 signature-string btw..

    Verify: 1

    I achived the same result with the following code:

    #!/usr/bin/perl use Crypt::OpenSSL::RSA; use MIME::Base64; Test_OpenSSL(); sub Test_OpenSSL { print "OpenSSL_Sign - Start\n"; my $signnature = OpenSSL_Sign("value1", "value2", "value3"); print "OpenSSL_Sign - Result: ".$signnature."\n"; print "OpenSSL_Sign - Done\n"; print "OpenSSL_Verify - Start\n"; my $verify = OpenSSL_Verify("value1", "value2", "value3", $signnat +ure); print "OpenSSL_Verify - Result: ".$verify."\n"; print "OpenSSL_Verify - Done\n"; } sub OpenSSL_Sign { my $param1 = $_[0]; my $param2 = $_[1]; my $param3 = $_[2]; # instead of having a function that reads the file to a string i j +ust declare a variabele with the content of the file just to make it +more clear.. my $private_key_string = qq^-----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEA1Hd90G9/IAvZleTi86cqb8ZdM6vzOsv/Fxs/OpaHYexKs+t2 rg+f/h7hiTakXvRhN1E+N1GZ2jXSCNKniqPKaamspIqGbbylBoDm353o6DULZi7N Vm/GG5+LJwZFwzTPFuH248k0sxwTjcioj0ho06yLWPvkUXiFQslHuno6g59hC5gC n9Lh89ULkSes/7BZTXtD1mVywEnvfdRIN8rH6Kbt+RlrYP+CzmENmdQ4jKEj7aYB Y3+qjsmL4D65De6OChb21i/iyhiPhdWez8+ulPRoinxMsbnWslgiWXifVueyplL8 wDDhx5yYXUwha3FYqXTSq0qk7ZsAET+ftoQ1OwIDAQABAoIBAQCu5wb9VSuzd83Q pTFnHo/Cn+sNpFmAZ3pAzT9Jva0JIXmFjyqNs3MjwMwtJnw9ZrO40/qBk30xsuiK Ns+RQ7BXN0RV12s+XvMC3Y4xO08GCNSvc4u5wh21k6r7nyBC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fHL45mR0MZKawHZ+5XaLH4rPK1AA== -----END RSA PRIVATE KEY-----^; my $rsa_priv = Crypt::OpenSSL::RSA->new_private_key($private_key_s +tring); my $plaintext = $param1 . $param2 . $param3; my $signature = $rsa_priv->sign($plaintext); return encode_base64($signature); } sub OpenSSL_Verify { my $param1 = $_[0]; my $param2 = $_[1]; my $param3 = $_[2]; my $signature = $_[3]; # instead of having a function that reads the file to a string i j +ust declare a variabele with the content of the file just to make it +more clear.. my $public_key_string = qq^-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1Hd90G9/IAvZleTi86cq b8ZdM6vzOsv/Fxs/OpaHYexKs+t2rg+f/h7hiTakXvRhN1E+N1GZ2jXSCNKniqPK aamspIqGbbylBoDm353o6DULZi7NVm/GG5+LJwZFwzTPFuH248k0sxwTjcioj0ho 06yLWPvkUXiFQslHuno6g59hC5gCn9Lh89ULkSes/7BZTXtD1mVywEnvfdRIN8rH 6Kbt+RlrYP+CzmENmdQ4jKEj7aYBY3+qjsmL4D65De6OChb21i/iyhiPhdWez8+u lPRoinxMsbnWslgiWXifVueyplL8wDDhx5yYXUwha3FYqXTSq0qk7ZsAET+ftoQ1 OwIDAQAB -----END PUBLIC KEY-----^; my $rsa_pub = Crypt::OpenSSL::RSA->new_public_key($public_key_stri +ng); my $plaintext = $param1 . $param2 . $param3; my $verify = $rsa_pub->verify($plaintext, decode_base64($signature +)); return $verify; }

    This is just an example code which I've made as close as the original as as possible but ofc, with random-names and values of the parameters. I've worked over 45 hours in 4 days so I'm a bit tired..

    Again, thanks for clarify the new_private_key and new_public_key funtions. The documentation on cpan was pretty much useless for a noob (on this topic) like me.

      i have the same problem with OpenSSL sign.. I have the code in php and i try to do it in perl and this your code is really helpful for me!