RSAKey::applyToValue description wrong?

In the description for RSAKey::applyToValue it states:

Call this on the public key object to encode some data, then use the matching private key object to decode it.

Shouldn't this be reversed?  You distribute the public keys to be able to decode.

No, you encode using the receiver's public key. His private key is usually unknown to you.

You distribute your public key in order to receive encrypted messages.

...but you are right, in the OnlineUnlockStatus the public key is used to decrypt. Is this a correct use?

The public and the private keys are not interchangeable, because the public key can be derrived from the private key (with little effort), but not vice versa.

But can you encrypt using a private key and decrypt with a public key?

from modules/juce_tracktion_marketplace/marketplace/juce_OnlineUnlockStatus.cpp:

void OnlineUnlockStatus::load()
{
    [...]

    KeyFileUtils::KeyFileData data;
    data = KeyFileUtils::getDataFromKeyFile (KeyFileUtils::getXmlFromKeyFile (status[keyfileDataProp], getPublicKey()));

    [...]
 }

[...]
static XmlElement getXmlFromKeyFile (String keyFileText, RSAKey rsaPublicKey)
{
    return decryptXML (keyFileText.fromLastOccurrenceOf ("#", false, false).trim(), rsaPublicKey);
}
1 Like

Can anybody enlighten my why it is a NOT a good idea to ENcrypt with a private key and DEcrypt with public key (aka signing)
I might makes sense for a licence scheme, but after some research the popular opinion is: that it is not a good idea, because “private key is not made for encryption”.

So what is the real problem? How safe is the data which was encrypted with a private key?

It’s safe for the time being, but nowadays you probably want a key size of at least 4096 bit. In general the main issue is performance, so private keys are in practice usually used for signing a SHA256 digest of a message or similar for proof of authenticity. For confidentiality/encryption of data of arbitrary size you want AES or similar designed for speed.

You mentioned signing. The private key is meant for signing a message and the public key can verify that signature. The public key is meant for encrypting a message that only the owner of the corresponding private key is able to decrypt.

For a public-key license scheme you usually sign a license on your server, and then use the embedded public key in your software to verify that the signature of the license is correct. A hacker can replace the embedded key with his own to create a keygen. Tip: store the key in such a way that it’s not possible to do this easily. (You could use a curve fitting tool to convert parts of the key into a mathematical function only evaluated at runtime.)

I probably went way off-topic here, sorry about that! :smile:

Edit: On a second thought I realize you probably know most of this already, but I’ll leave it in case someone find it useful.