Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi Monks,

I could swear I had code that was working several months ago; now it throws the same error no matter what I try. The error is:

"Failed to decode encrypted text (RSA.xs:202: OpenSSL error: oaep +decoding error at doit line 95.)</code>"

Any ideas? Here's an SSCCE of the problem:

#!/usr/bin/perl ############### ## Libraries ## ############### use strict; use warnings; use feature qw{ say }; use Crypt::OpenSSL::RSA; use Data::Dumper; use Function::Parameters; use IO::File; ################## ## User-defined ## ################## my $pubfile = "./key.pub"; my $privfile = "./key.priv"; my $keysize = 1024; my $message = 'A quick brown fox jumps over the lazy dog.'; ################## ## Main Program ## ################## create_rsa_keys(); # Create keys, save to dis +k my $pub = pubkey_from_file($pubfile); # Construct public key fro +m disk my $priv = privkey_from_file($privfile); # Construct private key fr +om disk my $enc = encode($pub, $message); # Encode the message my $dec = decode($priv, $message); # Decode the message say "Result '$dec'"; # Results (if error is fix +ed!) ################# ## Subroutines ## ################# fun create_rsa_keys() { # Generate the public & private key text, and save to disk my $genkeys = Crypt::OpenSSL::RSA->generate_key($keysize); my $pubtext = $genkeys->get_public_key_string(); my $privtext = $genkeys->get_private_key_string(); key_to_file('Public key', $pubfile, $pubtext); key_to_file('Private key', $privfile, $privtext); } fun key_to_file($label, $path, $text) { my $fh = IO::File->new; open($fh, ">", $path) or die "Failed to write $label to '$path' ($ +!)\n"; print $fh $text; close($fh); } fun pubkey_from_file($path) { my $text = file_to_string('Public key', $path); my $pubkey = ""; eval { $pubkey = Crypt::OpenSSL::RSA->new_public_key($text) }; $@ and die "Error in string_to_public_key ($@)\n"; return $pubkey; } fun privkey_from_file($path) { my $text = file_to_string('Private key', $path); my $privkey = ""; eval { $privkey = Crypt::OpenSSL::RSA->new_private_key($text) }; $@ and die "Error in string_to_private_key ($@)\n"; return $privkey; } fun file_to_string($label, $path) { local $/ = undef; my $fh = IO::File->new; open($fh, "<", $path) or die "Failed to read $label '$path' ($!)"; my $text = <$fh>; close($fh); return $text; } fun encode($pubkey, $plain) { my $encode = ""; eval { $encode = $pubkey->encrypt($plain) }; $@ and die "Failed to encode plaintext ($@)\n"; return $encode; } fun decode($privkey, $encode) { my $decode = ""; eval { $decode = $priv->decrypt($encode) }; $@ and die "Failed to decode encrypted text ($@)\n"; return $decode; } __END__ The public key file looks okay: -----BEGIN RSA PUBLIC KEY----- MIGJAoGBAMjDisZYXREArFdv5VrP/U1tzh7NShzQgVBN4Kb+2xNwkIhPZd3hv98f w3p/9IigAFhUBnXnNSnOsvZJ79/M8WZ5fOoQQzre8jyw84Z2H/nQsLZCkkN4n52H Byg+K5dSNCh3K0UdTN/Li6nbS19OTweGz3X+oKNPb+oZtwPQ5C+TAgMBAAE= -----END RSA PUBLIC KEY----- As does the private key file: -----BEGIN RSA PRIVATE KEY----- MIICXQIBAAKBgQDIw4rGWF0RAKxXb+Vaz/1Nbc4ezUoc0IFQTeCm/tsTcJCIT2Xd 4b/fH8N6f/SIoABYVAZ15zUpzrL2Se/fzPFmeXzqEEM63vI8sPOGdh/50LC2QpJD eJ+dhwcoPiuXUjQodytFHUzfy4up20tfTk8Hhs91/qCjT2/qGbcD0OQvkwIDAQAB AoGASMkb9uzyUMe9s7WNoW7mlrvIjsI+rFOrjkGtwN2E73dP7xtaAydlOK97X14k eaQIe/16miRI0e9uUnxbyveyTJ9PlRdwMfI5LbyaLTonRVjKfwOiDQ5HL4TsZmVX xCp4wht3v84Norh1+jytO0+QdiUMx/ilt31xRv9z4kqCzwECQQD8FW6QWkGtLXzT mGxCGN1zDSzrHowwCPhBpBogrQj+35y/rxUqoVzA/KeCXNPPJReroB6icKSGLjv9 Q8jvBjv5AkEAy+IBMveGh1OpZECe5bbIPQ8GLCumNp04XLQhDzSrm5hVBf0vtoiC HRVFuTe5jz3fa3G+ZCRxkHIS86YRAjiy6wJBAKyX4+5zzXnLpiaduql6qsxmHfYR ITyWN5uBxt3Oe1U+Nu1K67wXZRFBK1NnSSIPrGg2piLj6tFwAGTEL2PwKckCQCW6 zT4BUIP6l35V9xHLoYKrJRlHeprgvW1qaDPIK3m/1vwkvo+o82suZJjCFzTK3m2j vvgJRnrUoW01bjBbDcECQQCwKDz5kum6VjtcKtRHldADUuVPY4obt9FZ7PlxUMTK S6emRbu41Rbi/LrgYbaZSrJS1WANviuoXaZesPJPRR9l -----END RSA PRIVATE KEY-----

say  substr+lc crypt(qw $i3 SI$),4,5

In reply to Errors in Crypt::OpenSSL::RSA (from 'RSA.xs') by golux

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-26 05:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found