I'm setting up a system to update software on a device and I would like to encrypt the update files using a private key that I have created and then decrypt them with the public key. All of the examples that I have found encrypt with the recipients public key, I want to encrypt with my private key. I have seen Bob & Alice examples that use encryption of a file with your private key and then with the recipients public key, but I can't figure out how to do the private key encryption.

  • Do you want to be the only one to be able to decrypt the files ?
    – Vinz
    Nov 3, 2015 at 19:27
  • Maybe this will help you : crypto.stackexchange.com/questions/2123/…
    – Vinz
    Nov 3, 2015 at 19:37
  • In my scenario I don't really care if anyone can decrypt the file, although the public key will only be distributed to the systems that need it. What I care about is that I am the only one that can encrypt the file that contains the code updates. I suppose that I can just reverse the definitions in my case, and never disclose the 'public' key and put the 'private' key on the systems to be updated.
    – GWLindberg
    Nov 4, 2015 at 21:36

2 Answers 2


This is not the way GPG works.

You encrypt a file with a public key (may be yours), and decrypt it with a private key.

So if you share your public key on the Internet, anyone would be able to encrypt a file with your public key, but you would be the only one able to decrypt it.

See : http://www.dewinter.com/gnupg_howto/english/GPGMiniHowto-1.html

  • I suppose that the function that I'm using the encryption for could be done with signing where the file and the sign are concatenated to one file, as I don't care it anyone sees what the data is, just that it can be verified that I'm the one that generated it.
    – GWLindberg
    Nov 4, 2015 at 21:40
  • darn, broken link
    – odigity
    Mar 11, 2023 at 19:38

Old question, but... I came across it looking for something else.

What you really want here is not to encrypt the file, but to create a verification that you are the origin of the file. This is the purpose of signatures of files.

A quick basic idea of how public key encryption works: Encrypting something with your private key can only be decrypted by your public key. Encrypting something with your public key can only be decrypted by your private key. (Why this is so is something beyond the scope of this answer.)

In normal operation, if someone wants to send you a message, they encrypt it with your public key, since only your private key can decrypt it.

A signature generates a one-way hash of the file/data, and reverses the roles of the public and private keys: Anyone with your public key can decrypt the hash, and they can be certain only you generated the encrypted has because only your private key can be used to encrypt it in the first place.

The hash is used to verify the file has not been tampered with since you signed it. Any changes to the file will change it's hash, and make it clear hat it has been altered. Since they do not have your private key, they cannot generate a new signature of the new hash for decryption with your public key.

Many systems use this method to sign checksum/hash files of other files, assuring that the files with those checksums/hashes are authentic.

You can also combine data/file encryption and signing it, assuring that the encrypted data cannot be decrypted by anyone but the intended recipient, and that none of the encrypted data has been tampered with.

"Under the hood", this is how GPG and related programs work. It is too computationally expensive to encrypt the data using the key data. Instead, they generate a random "one time password" (OTP) to encrypt with a much faster encryption method. Then the password is encrypted with the keys of he intended recpients and sent along with the encrypted data. Since only their private keys can decrypt the password used to encrypt the data, you have very reasonable assurance no one else can.

Final note: Cryptography is always in a state of flux. What is "reasonable" or "assured" as I write this, might be invalidated tomorrow by someone discovering a weakness in one of the algorithms used.

You must log in to answer this question.

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