Hello all! I ask you to help me understand what I am doing wrong.

I created two scripts:

RSA_encrypt_test.pl - encryption of plain text "Hello!!!" in the file "puzzle"

RSA_decrypt_test.pl - decryption of the ciphertext in the file "puzzle.encrypt"

I provide both scripts below.

#! /usr/bin/perl # RSA_encrypt_test.pl use strict; use Crypt::OpenSSL::Bignum; use Crypt::OpenSSL::Random; use Crypt::OpenSSL::RSA; use Data::Dumper ; my $param_ref = { n => 'B99C5E09B0B215A4F65D34F4AF763F254D1094C1356951F40A6EDCBB5779 +A540C1EA363568002F7331A71085D3207C68B5B51C9F010272186261A181D65A80E9' +, e => '10001', d => 'EC8AD0EDFFEFCD443D2C6023FEB37789137D65B18ADEC333F1200FF2CC15 +800C7C2FFA92AE70077DA2C4C6F1C3795EA00087E4E97A5BCF5CEAEDD1567841C46E' }; my $n = Crypt::OpenSSL::Bignum->new_from_hex($param_ref->{n}); my $e = Crypt::OpenSSL::Bignum->new_from_hex($param_ref->{e}); my $d = Crypt::OpenSSL::Bignum->new_from_hex($param_ref->{d}); my $rsa = Crypt::OpenSSL::RSA->new_key_from_parameters($n, $e, $d); my $target = "puzzle"; open my $OUTF, '>', "$target.encrypt" or die "Error opening file $targ +et.decrypt: $!\n"; binmode $OUTF; open my $F, '<', "$target" or die "Error opening file $target: $!\n"; binmode $F; # ---------------- Checking by padding ------------------ #$rsa->use_no_padding(); #$rsa->use_pkcs1_padding(); #$rsa->use_pkcs1_oaep_padding(); #$rsa->use_sslv23_padding(); # ------------------ Checking all keys ------------------- #print $OUTF "private key is: %s\n", $rsa->get_private_key_string(); + #print $OUTF "public key is: %s\n", $rsa->get_public_key_string(); #print $OUTF "public key (in PKCS1 format) is: %s\n", $rsa->get_public +_key_string(); #print $OUTF "public key (in X509 format) is:\n", $rsa->get_public_key +_x509_string(); while (read $F, my $buffer, 8) { print $OUTF $rsa->encrypt($buffer); } close $OUTF or die "Error closing $target.decrypt: $!\n"; close $F or die "Error closing $target: $!\n";

-----------

#! /usr/bin/perl # RSA_decrypt_test.pl use strict; use Crypt::OpenSSL::Bignum; use Crypt::OpenSSL::Random; use Crypt::OpenSSL::RSA; use Data::Dumper ; my $param_ref = { n => 'B99C5E09B0B215A4F65D34F4AF763F254D1094C1356951F40A6EDCBB5779 +A540C1EA363568002F7331A71085D3207C68B5B51C9F010272186261A181D65A80E9' +, e => '10001', d => 'EC8AD0EDFFEFCD443D2C6023FEB37789137D65B18ADEC333F1200FF2CC15 +800C7C2FFA92AE70077DA2C4C6F1C3795EA00087E4E97A5BCF5CEAEDD1567841C46E' }; my $n = Crypt::OpenSSL::Bignum->new_from_hex($param_ref->{n}); my $e = Crypt::OpenSSL::Bignum->new_from_hex($param_ref->{e}); my $d = Crypt::OpenSSL::Bignum->new_from_hex($param_ref->{d}); my $rsa = Crypt::OpenSSL::RSA->new_key_from_parameters($n, $e, $d); my $target = "puzzle.encrypt"; open my $OUTF, '>', "$target.decrypt" or die "Error opening file $targ +et.decrypt: $!\n"; binmode $OUTF; open my $F, '<', "$target" or die "Error opening file $target: $!\n"; binmode $F; # ---------------- Checking by padding ------------------ $rsa->use_no_padding(); #$rsa->use_pkcs1_padding(); #$rsa->use_pkcs1_oaep_padding(); #$rsa->use_sslv23_padding(); # ------------------ Checking all keys ------------------- #print $OUTF "private key is: %s\n", $rsa->get_private_key_string(); + #print $OUTF "public key is: %s\n", $rsa->get_public_key_string(); #print $OUTF "public key (in PKCS1 format) is: %s\n", $rsa->get_public +_key_string(); #print $OUTF "public key (in X509 format) is:\n", $rsa->get_public_key +_x509_string(); while (read $F, my $buffer, 64) { #print $OUTF $rsa->public_decrypt($buffer); print $OUTF $rsa->decrypt($buffer); } close $OUTF or die "Error closing $target.decrypt: $!\n"; close $F or die "Error closing $target: $!\n";

The RSA_encrypt_test.pl script encrypts the contents of the file "puzzle" without problems and creates the file "puzzle.encrypt" But the problem is that the RSA_decrypt_test.pl script cannot decrypt back. This works without error, but no result.

I have tried various types of padding but no result.

I ask everyone who has worked with RSA to help. What's my mistake?


In reply to RSA encrypt but no decrypt. What is the problem? by Forb

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.