1

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.

3

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.

  • 1
    It makes no sense... Disagree. Consider deployment of a secure package to multiple endpoints from a guaranteed single source. The encrypted package provides both content security and, intrinsically, the signature. Alternately, the package is encrypted with a symmetric key which is protected asymmetrically then bundled together for delivery. A wholly, asymmetrically encrypted package is preferable -- even if not possible today. – bvj Apr 1 at 19:36
  • @bvj Then you aren't encrypting with a private key, as in, the private part of a key pair for an asymmetric cryptographic scheme. You're encrypting with a secret key. (Terminology isn't completely standardized, but most of the world uses “private key” only in the context of public-key cryptography, and uses “secret key” in the context of symmetric cryptography.) With public-key cryptography, if you're using a private key, you're either decrypting or signing, not encrypting. – Gilles Apr 1 at 19:56
  • Thanks, @Gilles. Notwithstanding convention, isn't it possible via RSA to encrypt with the private key and subsequently decrypt with only the public key? The endpoints I referred to would be securely configured with the public key. Thereafter, they'd receive payloads that could certainly be signed with the sender's private key, but preferably receive an additional symmetric key encrypted by the sender's private key. The symmetric key could then decrypt the protected payload. Signing is essential, obscurity is preferable. The endpoints could be IoTs for example. – bvj Apr 1 at 20:35
  • @bvj No: you encrypt the symmetric key with the recipient's public key. Even if you could encrypt with the sender's private key, anybody could decrypt it with the corresponding public key, so that would make the encryption pointless. Encrypting with a private key, or signing with a public key, is technically possible with RSA (but typically not with other public-key algorithms, as in, you can perform the mathematical operation, but it doesn't have any interesting security property. – Gilles Apr 1 at 20:52
  • So basically two sets of key pairs are required where both the sender and recipient each have their own private key. Thank you, Gilles! – bvj Apr 1 at 21:14
6

Use OpenSSL to do that. Follow a simple example:

To encrypt a file:

openssl rsautl -encrypt -inkey public_key.pem -pubin -in <decrypted file> -out <encrypted file>

To decrypt a file:

openssl rsautl -decrypt -inkey private_key.pem -in <encrypted file> -out <decrypted file>
  • 1
    He is asking how to encrypt using Private Key. – Ibrar Ahmed Sep 21 '18 at 18:52
0

You're speaking of an app. That means you probably want to use a library, not a command line. How to do that is out of scope for UNIX & Linux SE (and you didn't even tell us what language your app is written in), but there are several libraries which can do what you want:

  • OpenSSL's libcrypto has several primitives to deal with signature verification
  • GPGME is a library written by the author's of GnuPG, which allows many things, among them signature verification.
  • GnuTLS also exposes its internal cryptographic primitives, however they don't recommend it.

I'm sure there are more, these are just the most popular solutions. Most of these libraries have bindings for other languages, too, in case you're not using C.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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