A a total noob to cryptography.

Situation: Given only the public_key i need to know if it is valid; in that, if i use it to encrypt say a form submission, the form data will actually be encrypted.

What am interested in finding out and need help with is:

Python tool/library and how it maybe used to validate the said public key

Update: so thoughts i have on this is that i can:

  • check that the PEM string (public key) can be base64-decoded without errors
  • Also that the key can be openssl parsed into an RSA structure, i.e. check that it contains a modulus and exponent.

Would this be a good enough check to validate the RSA public key string?

  • ssl library does that Commented Jun 7, 2019 at 13:43
  • The key represenations readable by OpenSSL (public and private too) are all ASN.1 DER or PEM of ASN.1 DER (and PEM is not just base64, it is base64 with linebreaks and labels and yes those matter). ASN.1 DER has some redundancy and fake-PEM with random base64 would be very unlikely to parse. OTOH no flaw or attack would ever produce random base64; a flaw would usually produce only slight differences from a valid key, and an attack would usually produce a completely wrong but valid key, and neither parsing nor the 'sanity check' would catch any problem that occurs in the real world. ... Commented Jun 10, 2019 at 16:12
  • ... That's why in the real world we mostly use 'PKI' (public key infrastructure), mostly in the form of X.509 certificates (aka SSL/TLS certificates and SMIME/email certificates) or at least PGP signed keys instead. See wikipedia, or hundreds of existing Qs. Commented Jun 10, 2019 at 16:12

1 Answer 1


The short answer is no, but you're probably asking the wrong question.

There are sanity checks you can make on data that represents an RSA public key, but they don't validate that the public key “actually encrypts”. They validate that the data represents a mathematical object for which the RSA calculations will work (e.g. won't cause a division by zero or an infinite loop).

if i use it to encrypt say a form submission, the form data will actually be encrypted.

If you mean that the form content should be encrypted in transit and that the server should be able to see the plaintext, this doesn't involve RSA at all. Check whether the HTTPS protocol is used for the form submission. The page that displays the form should be HTTPS as well, as well as any other page where you access the site while logged in with the same account. With HTTPS, the contents are encrypted and protected against a man-in-the-middle.

If you mean that the server shouldn't be able to decrypt what you send, why are you sending that data in the first place? But ok, let's assume that you want to send encrypted data to a server, and that the server shouldn't be able to decrypt it.

If you've used RSA correctly, then the only entity that can decrypt the data is whoever has the private key. It's impossible to tell, by looking only at the public key, who has access to the private key.

If you have the private key and you generated the public key from the private key, you don't need to do any validation on the public key. You know it's valid because you obtained it by a correct procedure.

If you don't have the private key and someone gave you the public key, you have to trust them to keep the corresponding private key safe. You can't tell by looking at the public key.

So no, there's no check on the public key that will tell whether your data is securely encrypted. You need to review your whole system architecture.

  • thanks for answering, what i would like clarification on is, There are sanity checks you can make on data that represents an RSA public key ,what is an example of sanity check?, also; if these sanity checks pass can the public key be termed as valid, i think my question should have been what differentiates a random base64 encoded string to a RSA public key, how do you tell the two apart, is there a way?
    – Pnet
    Commented Jun 10, 2019 at 12:31
  • You also can't check in general whether the key was generated in a way that makes it weak/breakable, either deliberately or accidentally, although you can check for a few known cases like the Debian broken-RNG bug and the shared primes found by factorable.net and similar searches. @Phet: the sanity check is that n is a large (of the expected size) odd positive integer, and e is a positive integer from 3 to at most n/2 and usually smallish like 3, 17, 65537. Commented Jun 10, 2019 at 16:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .