Forb has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|