PHP function Openssl_Public_Encrypt equivalent in VB

I have an PHP script that communicates with an external system.  No idea who wrote the script.  It basically works like this -
1.  Sends a request containing a json text asking server for public key
2.  Server responds with a json contain a key
3.  Use the key to encrypt the request for server to send data

I've successfully coded the parts that ask for and retrieves the key.  The part about encrypting the data request has me stumped.

The PHP script has gets the key response from the server and decodes it the base64_decode.

         $public_key = base64_decode($response->result->date->params->public_key);

It then calls a function that creates the encrypted key to return (This is where I need help)

function getKey($public_key) {
$utc_timestamp = strtotime(gmdate('Y-m-d H:i:s'));
$y = date('Y', $utc_timestamp);
$m = date('m', $utc_timestamp);
$d = date('d', $utc_timestamp);
$data = sha1($y . STATIC_KEY . $d . $m);
$encrypted_key = null;
// encrypt authentication key using public key from DTD - different for each unit
if (openssl_public_encrypt($data, $encrypted_key, $public_key) === false) {
throw new Exception("Public encryption fail", 40);
return $encrypted_key;

Open in new window

I can decipher most of what is happening in the function, but I can't find any equivalent in VB for the openssl_public_encrypt function.

Anyone have an idea on how I can do this?
David KnutsonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
David KnutsonAuthor Commented:
Julian - I looked at it, but couldn't see how it will help me.  Any suggestion?
Julian HansenCommented:
From article found googling "X509Certificate2 openssl_public_encrypt"

Might be of use
X509Certificate2 myCert2 = null;
RSACryptoServiceProvider rsa = null;

    myCert2 = new X509Certificate2(@"C:\mycertprivatekey.pfx", "password");
    rsa = (RSACryptoServiceProvider)myCert2.PrivateKey;
catch (Exception e)
    Console.writeln(e.message); // because I left a blank catch block, I did not realize there was an exception! I missed the password for the certificate.

byte[] test = {Convert.ToByte("123")};

string t = Convert.ToString(rsa.Decrypt(rsa.Encrypt(test, false), false));

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

David KnutsonAuthor Commented:
Sorry if I'm being dense, but the X509Certificate2 method wants a file.  There isn't a file involved in this communication between the api and me.

When I call the api at the remote site it returns a string like this - ($public_key in code above)


I have a string that looks like this "2015dS%8h419A0814" that is hashed using SHA1.  ($data in code above)

These two strings are then encrypted and I send the result back ($encrypted_key)  to the remote API.

I'm thinking its would be some  kind of RSA function, but I can't find anything that looks right.
David KnutsonAuthor Commented:
Ok, I'm getting closer.  A colleague provided me with some background on this.  He was able to create an app using  classes from BouncyCastle.  Unfortunately  - this is in C++ and I don't know that coding.    I have gone into his code and see that it is using RSA

Does anyone know of a way to code RSA in VB, or do I need to start learning C++?
Julian HansenCommented:
This link has some more info

There is another link that might be useful - I cannot post it due to policy restrictions on EE but if you google

"X509Certificate2 openssl_public_encrypt"

You should find it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.