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?
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:
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.