eMail cert in program.

Hi:
I have two Thunderbird client accounts One called webpass@xyz.us is in office. Other webmaster@xyz.us
is as indicated the webmaster for website. When someone (registered user) changes their password through a website interface, I am sending email to webpass containing the new password. I want to encrypt that email message. The is sent with sendmail (old v .8) as adapted by me.

The two TB email accounts use two different Ccomodo certs. I exchanged digitally signed messages and the two accounts are working fine exchanging encrypted messages.

On my website using sendmail, I am trying to use the public key for webpass to send an encrypted message from webmaster to webpass.

Using TB webmaster Account Settings | Cert Manager I found the cert from webpass and identified it:
“Not Critical
E-Mail Address: webpass@xyz.us

I exported that and am trying to use it to encrypt message to webpass. I exported it (.p12) and extracted certs using OpenSSL.

My program code to encrypt the message:

sub encryptMessage {
my ($public_key, $string) = @_;
warn(“public_key at line 208 ‘$public_key’”);
my $key_string;
open(my $pub, “<”, $public_key) || die “$public_key: $!”;
read($pub,$key_string,-s $pub);
close($pub);
my $public = Crypt::OpenSSL::RSA->new_public_key($key_string);
encode_base64($public->encrypt($string));
return $string;
}

The call to the above sub is

my $public_key = '/home/xyzus/public_html/httpsdocs/cgi-bin/perl/Mail/certs/publicCERT.pem';
warn("public_key at line 669  '$public_key'");
my $eMessage = encryptMessage($public_key, $mail{'Message'});	
 socket_write($CRLF, \$eMessage, "$CRLF.$CRLF")
       || return fail("send message error");
socket_read()
    || return fail("message transmission error ($_)");
$log .= "\nResult: $_";
# finish
socket_write("QUIT$CRLF")
       || return fail("send QUIT error");
socket_read();
close S;

return 1;

I have tried generating public_key over and over. Nothing works.

Going back to the .p12 export for webpass, what steps to follow to assure I have the “public key” I need?

Revised perl code:

sub encryptMessage {
my ($publickey, $string) = @_;

my $public_key = ‘/home/bwmjcmus/public_html/httpsdocs/cgi-bin/lib/perl/Mail/certs/publicCERT.pem’;
open(my $fh, “+<”, “$public_key”) or die “Opening: $!”;
my $key_string;
read($fh,$key_string, -s $fh);
warn (“line 214 key_string ‘$key_string’”);
close($fh);

my $public = Crypt::OpenSSL::RSA->new_public_key($key_string);
encode_base64($public->encrypt($string));
return $string;
}

Response:

unrecognized key format at /home/bwmjcmus/public_html/httpsdocs/cgi-bin/lib/perl/Mail/sendMail.pm line 226.

This is public key for target email