How to encrypt a file with private key

gpgopensslsignature

I want to encrypt a file with a private key and decrypt it with a public key. A public key will be embedded in my app. So I want to have a guarantee that the file was created by me. How can I use gpg or openssl to implement it.

Best Answer

It makes no sense to encrypt a file with a private key.

Using a private key to attach a tag to a file that guarantees that the file was provided by the holder of the private key is called signing, and the tag is called a signature.

There is one popular cryptosystem (textbook RSA) where a simplified (insecure) algorithm uses has public and private keys of the same type, and decryption is identical to signature and encryption is identical to verification. This is not the case in general: even RSA uses different mechanisms for decryption and signature (resp. encryption and verification) with proper, secure padding modes; and many other algorithms have private and public keys that aren't even the same kind of mathematical objects.

So you want to sign the file. The de facto standard tool for this is GnuPG.

To sign a file with your secret key:

gpg -s /path/to/file

Use the --local-user option to select a secret key if you have several (e.g. your app key vs your personal key).

Transfer file.gpg to the place where you want to use the file. Transfer the public key as well (presumably inside the application bundle). To extract the original text and verify the signature, run

gpg file.gpg

If it's more convenient, you can transfer file itself, and produce a separate signature file which is called a detached signature. To produce the detached signature:

gpg -b /path/to/file

To verify:

gpg file.gpg file

You can additionally encrypt the file with the -e option. Of course this means that you need a separate key pair, where the recipient (specified with the -r option) has the private key and the producer has the public key.

Related Question