#!/usr/bin/perl use Crypt::OpenSSL::Random; use Crypt::OpenSSL::RSA; use Crypt::OpenSSL::X509; use MIME::Base64; use strict; my $debug = 0; local $/ = undef; my $public_key= "/data/pubkey.pem"; open (FILE, $public_key) or die ("Cant open public key file"); my $public_key_string = ; close(FILE); my $private_key = "/data/private.pem"; open (FILE, $private_key) or die ("Cant open private key file"); my $private_key_string = ; close(FILE); my $public_cert = "/data/x509-public.pem"; open (FILE, $public_cert) or die ("Cant open certificate file"); my $certificate = ; close(FILE); print $certificate; my $cert = Crypt::OpenSSL::X509->new_from_string($certificate); print $cert->pubkey; print $public_key_string; # they're different $public_key_string=$cert->pubkey; # it's different to the one loaded from file, but still works with my existing private key in the rest of the code - remove this line to test the old $public_key_string value print $public_key_string; my $plaintext = "Some text here"; my $rsa_pub = Crypt::OpenSSL::RSA->new_public_key($public_key_string); my $rsa_priv = Crypt::OpenSSL::RSA->new_private_key($private_key_string); my $ciphertext = $rsa_pub->encrypt($plaintext); # encrypt with the public key my $plaintext_back = $rsa_priv->decrypt($ciphertext); # decrypt with the private key print "Plain text back is " . $plaintext_back . "\n"; # Works fine if ($debug){ # debugging only print "private key is:\n", $rsa_priv->get_private_key_string(); print "public key (in PKCS1 format) is:\n", $rsa_pub->get_public_key_string(); print "public key (in X509 format) is:\n", $rsa_pub->get_public_key_x509_string(); } $rsa_priv->use_md5_hash(); # use_sha1_hash is the default $rsa_pub->use_md5_hash(); my $signature = $rsa_priv->sign($plaintext); # sign with private key if ($rsa_pub->verify($plaintext, $signature)) { # verify with public key print "Signature Verified successfully"; } else { print " *** Not verified"; }