I've just been looking into RSA encryption with C++ and am wanting to make a program that can en/decrypt files of my own custom file extension. Obviously I need to choose two primes numbers (for my p and q values) to encrypt and decrypt this file, but I am curious as to how I could stop these values being pulled out of my program via de-compiling.

Obviously, with the primes in hand, someone could easily calculate the necessary values for the private key that ONLY this executable should have.

Are there methods for somehow generating a random private key that will always unlock the public key?

  • $\begingroup$ RSA is slow for file encryption, and you will need padding, too. Also, in RSA you cannot encrypt bits larger than the modulus. Please, see Veracrypt. $\endgroup$
    – kelalaka
    Sep 11, 2019 at 7:29
  • $\begingroup$ Why would anybody need to extract the key if your program will just decrypt stuff anyway? In any case as currently written this seems to be off- topic. $\endgroup$
    – Maeher
    Sep 11, 2019 at 7:50
  • $\begingroup$ True, it's pretty silly giving away th eonly method to decrypt a file... $\endgroup$
    – Zaxter5
    Sep 11, 2019 at 8:11
  • $\begingroup$ related Why does BitLocker not use RSA? $\endgroup$
    – kelalaka
    Sep 11, 2019 at 12:23

1 Answer 1


You can't. If the user can run the software, they can extract the key from it. There is research on how to make it difficult to extract the key (white box cryptography), but it's not very successful.

You need to think about what do you want to accomplish. Does the user encrypt the files to themselves, so that they can decrypt it later? Then you could derive a symmetric key from a user's password. Does the user want to encrypt the file to someone else? Then each user should generate a private key (the primes) for themselves, and send their public keys to one another.

(In each scenario there are a lot of details you should take care of, so you're just scratching the surface of the problem)

  • $\begingroup$ Thank you for clearing that up, I guess I wasn't thinking about the process really. Nothing much matters atm as I'm just messing around learning, but those are things I'll definitely think about in the future. $\endgroup$
    – Zaxter5
    Sep 11, 2019 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.