singpolyma / openpgp-php

OpenPGP.php is a pure-PHP implementation of the OpenPGP Message Format (RFC 4880).
http://singpolyma.github.io/openpgp-php/
The Unlicense
180 stars 70 forks source link

How to create a key and export it for encryption using GnuPG? #40

Closed cgraefe closed 7 years ago

cgraefe commented 8 years ago

In my current use case, I would like to use openpgp-php for key generation and decryption. The public key will be published and documents encrypted (somewhere else) using gpg should be read and decrypted. I generated a key pair using the keygen.php script from the examples folder and exported the public key, which I then imported into my gpg keyring using

gpg --import gpg_pubkey

When trying to use the key for encryption, gpg will skip it as an "unusable public key". I suppose this is because it is flagged for usage "C", only:

pub  1024R/DDF1E2FB  created: 2016-10-10  expires: never       usage: C
                     trust: full          validity: unknown

Is there a way to generate and export a public key for encryption? Or would I need a subkey for this? Any hints on this or maybe an additional/extended example script would be very much appreciated.

singpolyma commented 8 years ago

You can see where the default key flags are set here: https://github.com/singpolyma/openpgp-php/blob/master/lib/openpgp_crypt_rsa.php#L157

As of recent commits the defaults are S and C.

If you want to add E, you will need to pass sign_key_userid an OpenPGP_SignaturePacket in addition to the key and userid packets. Set up as you see for the defaults, but add whatever settings you want (for example, also or on 0x04 to say the key can be used to encrypt communications -- see more flags in RFC4880 section 5.2.3.21)