Requirements:
Essentially I have a series of devices (running NodeJS
) that need to maintain their own unique private and public keys. They communicate with a centralized server in PHP
by pulling content.
When a new device starts up, I would like it to generate a private and public key and send only the public key to the PHP server to be stored.
When a device runs a GET
request on the PHP server, the server should use the public key it was provided to encrypt the data.
When the device receives this response, it should be able to use the private key to decrypt this data.
Currently:
I am currently generating a private and public key pair using keypair. I send the public key to the PHP server to be stored and associated with the unique device.
I encrypt the data on the PHP server using EasyRSA:
$message = "Decrypt me if you can";
$publicKey = new PublicKey($storedPublicKey);
$encrypted = EasyRSA::encrypt($message, $publicKey);
return $encrypted;
With this encrypted string being returned to the NodeJS
app, I then try to decrypt it using NodeRSA (where response
is the string response from the PHP server):
const key = new NodeRSA(storedPrivateKey)
const result = key.decrypt(response)
However it errors out with:
Error during decryption (probably incorrect key).
I believe I am missing something fundamental here, but am unsure on what that may be. Any thoughts?