Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^5: Crypt::RSA Cant load private key

by grantm (Parson)
on Jan 10, 2012 at 08:39 UTC ( #947113=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Crypt::RSA Cant load private key
in thread Crypt::RSA Cant load private key

I just looked at some of my code that successfully uses private keys (for signing). And found code like this:

use File::Slurp qw(read_file); use MIME::Base64 qw(encode_base64); require Crypt::OpenSSL::RSA; my $key_text = read_file( $key_filename ); my $rsa_key = Crypt::OpenSSL::RSA->new_private_key($key_text); $rsa_key->use_pkcs1_padding(); my $bin_signature = $rsa_key->sign($plaintext); print encode_base64($bin_signature, '');


Comment on Re^5: Crypt::RSA Cant load private key
Download Code
Replies are listed 'Best First'.
Re^6: Crypt::RSA Cant load private key
by ulisescastillo (Initiate) on Jan 10, 2012 at 16:31 UTC
    Hi grantm, I tried your code and its the same problem
    message:
    RSA.xs:146: OpenSSL error: no start line at decryption.pl line 38.
    line 38 =
    my $rsa_key = Crypt::OpenSSL::RSA->new_private_key($key_text);
    It would be much to ask what version of module OpenSSL::RSA are you using?

    Does OpenSSL need that the --- BEGIN PRIVATE KEY --- headers be there? Im very sure those headers are there, because I've executed the command:

    $ openssl pkcs8 -inform DER -in pza000413788_1011171750s.key Enter Password:
    And I see the Headers:
    -----BEGIN RSA PRIVATE KEY----- -----END RSA PRIVATE KEY-----
    But what I ment is if should I have to change the format of the privateKey File or something. Any suggestion is really apreciated!

      DER is a purely binary format. If you base64 encode DER data, and add those -----BEGIN/END----- blocks you get something called PEM. It's pretty easy to convert between them:

      my $der = MIME::Base64::decode_base64( join "\n" => grep { !/^-----(BEGIN|END)\s.*-----$/ } split /\n/, $pem );
      I leave the conversion in the opposite direction as an exercise for the reader.
        This isn't the case.
        if I do base64 to the .key file what I get is:
        MIICxjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIyyJtIVAFvgECAggAMB QGCCqGSIb3DQMHBAgvf1KZF2K5qQSCAoAZwva/LcmuJWHZbsPyIUJlTUaQptlbTVYd K4eOspM8dIk5GlmPyi1K9kjrGyIXlLiJkmZTMCz7hCg72IBOnPYjEAlwQ8SBbST01S /vxO/oU7S3a6ReMMiXRBlWr5pcqORq0ovL85T3Pth9a7hnWdse87mVPC969e2QTBbC jqRRd+VcOUkq3h6Ko8dz/UWgVICrI225eZ+zC5Gt4Fw675Y0a9XdGDTbn42f3ciGXU r9Qy3P7ha5e3myqZa6pOy/H4aiaIQg+AY3L9O6yyW6aRuT9RSC7ijjq5wm1/CYYA6f /A4tOdez0vc9Rt0q/YZ1UazWI6lNzeRWINXLvN0OBGBrTavd7ypmMTlk71MDadaVTP op85Ve3aTIbkVtk8lQnbP5ItOtpkorrh5S1GxDHPvWMQZBwwQu8ndPrWdbmeT/xA8r VRCtpyNknnlTEDZREHXm+VVJoaImE2XPQ4juZAqzlykOrzkn+HBUYO1koLvti1SU7I GIryUcwtON7V2xMuoX6mMncNaW+LP0450H1YDgXTrbDBHJxx/1e+IYe4Hjbn5YtBfP 38vKrgc4qNG9pCcD4Zy603raqIuPaK0imH79koRr0ZfiKO9/9w8L/2JoJU1+GE+C95 mDfddmXX9ZtsZZINk0Q+/zN73gAWfa92QNvIydTjfblI+cEYw3AfZWSLrEapl6sycR NDzcY5o+KQIW5MPBtaWjvBCyRdcPAIbmZgZPwYZTVkcwTRfYzhwLu7Kwhf2MY93vc/ VpXqDF89wxst6R6lJur3UAZU4IHbIAkPy9iiwNVrmNQlPEOW7eRzsva1HtDgXJc1ma 46lgEqdZZqkgUS+cZlElknPxI26K

        and if I do the complete openSSLComand I mentioned before I get:
        user@user:~/$ openssl pkcs8 -inform DER -in aaqm610917qja_1011180955s.key Enter Password: -----BEGIN RSA PRIVATE KEY----- MIICWwIBAAKBgQCcphXGAbrbUnaumkSTsbGrFIfkaajOpvP1RFcVcbpWe7JBNXAw ShKIH79QGLYEc9ATBmlxtjAma0B4ZRBTjmQ4vQrp9LwT3bCNX+9J9lUOHGsCysya u3VxGNoCbhBxMYQP835LjAcy1d4AScOjGx8hxTZ6AUXtMmyEe+0NNQsJnQIDAQAB AoGAF4PfFOBBmpbgdgl2be1ozFPCqokp7Aun55OgtvlhjYX4Fk3dT4JvEbwiTfXH rRdtjs2Rmp5M0Bedj/Ur5D8l0BqBQznSrm6JGaDMv40wOk265qoECda/zZ2tdjvU xmcYpuu2AE4mdvqVsMqDOVCSp/5aBUL+hv54OtADuVi/J0ECQQDIa1DW9ct2FsQT h6sNdvgjI35fJ/MKA0y/kLD0wbwHT1VbUE3GevHHSeYOccKnWErlw4ppt9jglEvt m/n1BlOlAkEAyBdN4sKUxbTy1NobPgQJ2UsmaCNU7JN9+wIgJ8gePkRktsHLE+ec yBhBki3rYvexbXlmOU94yfrzzPZuyBkcmQJAfAr/o6vJW7NHdBb55YGGnqjvJBHj uITGUaJKA5KMv7F4UOXo3Tlmv0ObocEDCvP4lzBpUHk7P/RI8i0hjWd3CQJAal0p A//XwflclJWUobTX33C5a+ZMQK5u673Hhh41JXiJ6TS/VUdr6loqkSBj3mo5IS7F jnWWihgi/bOKVEg1EQJACsF3shzuBETh5dyoqBJRP7EbzxB8SaeQapa9lJlkrOKD HAwVS32OtPWCmU3isOjbqh+Mc3fNRe0Y9xq+nsingg== -----END RSA PRIVATE KEY-----

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://947113]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2015-07-31 11:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (276 votes), past polls