I am trying to use gpg generated public keys to do encryption using libgcrypt. I used the list packets command on the key to get me the contents of the key and parsed the mpi values into an s-expression. So when I try to encode my session key with my public key I get the error "Odd hexadecimal numbers in S-expression"? I think list packets is maybe giving me hex numbers without the leading zero? If so what do I need to do to get me mpi values that I can use in libgcrypt?
问题:
回答1:
After trial and error and advice from the gpg and libgcrypt mailing lists, the list packets command does truncate the leading zeros from the mpi value and as a result you would have to pre-pend the mpi values with zero(s) when:
- the length of the hex string is a odd number (pre-pend with one zero) or
- the mpi is a negative value (pre-pend with two zeros).
As a further note, it is not wise to use list packets to get mpi values as it does not reliably gets you mpi values as it is supposed to be a debugging command which is subject to change. The best way that I have found to get the mpi values for both the public and private keys is to directly parse the binary export of the key by using export key command with RFC4880 to decode it.
回答2:
you can use this to export the secret key:
gpg2 --homedir . --export-options export-sexp-format --export-secret-key $KEYID
and this to export the public key:
gpg --homedir . --export | openpgp2ssh $KEYID | ssh-conv | sexp-conv --syntax=hex
you may need the following packages for the tools:
sudo apt-get install monkeysphere lsh-utils nettle-bin
回答3:
I was able to get all the mpi values I needed by using pgpdump. Be sure to use -i
which tells it to print all the integer values. The only curious thing is that the public key command that Attilla posted had two zeroes in the beginning that the pgpdump output did not have. I'm not sure if they are needed yet.