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

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

try
{
    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

0

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
Cloud Class® Course: 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)

DiUIf2VzbHWJmO2vJVW7uErUhodfRI1Tw5jGT0k4n4XB1MGhvWIKD
tqx83TEexSryeSTg6gcdcea4ocsHdynTynu1sfo1kPx+IxvfYATJxaEzm8LyeGro1X5yfOQRDwJPyFine7
jUJ6z8W47H3Oj3VNXB6Qb7bfxsHhMhuJT9JygDGj0Mvin0jichsELuMj5W\/OGKUQmnOJ09DjbgEanz3s9
lUMKcSSBnKyBcSvlf6BrznDyWqHeeu5jGAfNn6d2alpmGtOGnlQQBWqm+dzbmUZmqXcb0PhqR+2lMj0rni
HoCyKpDjorxvSZ96qiG6nSJrC5wxuhQB9f9gmEFjaleg==

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.
0
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++?
0
Julian HansenCommented:
This link has some more info

http://paulstovell.com/blog/x509certificate2

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.
0
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
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.