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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.