简体   繁体   中英

Encrypt and decrypt using bouncy-gpg and PCKS12 key

I have trouble with the implementation of encryption and description using bouncy-gpg and PCKS12 key. Now I have PCKS12 key (extension: .p12). From this key, I am able to get the public and private keys. Otherwise, the bouncy-gpg requires to use gpg keys. How can I use p12 file in bouncy-gpg? That would be appreciated if any advice. Thanks.

Meta: Stack convention is to post code as text formatted as code, which means it must be in a question or answer not a comment because comments can't support that formatting. Although I personally don't care much, a lot of the community will complain that images (1) can't be cut&pasted by people who want to work on your problem, (2) aren't indexd, and (3) aren't accessible to visually impaired users, and possibly those with limited devices. Plus, convention is to post the question in the question, not in comments, because comments can be and often are deleted without trace.

More substantively, I didn't appreciate that you meant specifically this bouncy-gpg not (just) the more general concept of "BouncyCastle plus GPG". That took me longer because I'm familiar with most of BouncyCastle but not at all bouncy-gpg. That said,

it's the PGP confusion between private and secret again . BouncyCastle's PGPPrivateKey is usable in BouncyCastle , but it is not the same as the external key; for historical reasons, the PGP format for what is really a private key or subkey is called instead a secret (sub)key, and corresponds to BouncyCastle's PGPSecretKey -- even though the file when armored uses the label PGP PRIVATE KEY BLOCK (!) What you got from PGPPrivateKey.getPrivateKeyPacket() isn't really a packet at all, only the part of the secret-key packet that is password-encrypted. To get a proper packet you need something like:

    // get prv,pub from the PKCS12 KeyStore as now
    JcaPGPKeyConverter cvt = new JcaPGPKeyConverter();
    PGPPublicKey pub2 = cvt.getPGPPublicKey(PGPPublicKey.RSA_GENERAL, pub, new Date());
    PGPPrivateKey prv2 = cvt.getPGPPrivateKey(pub2, prv);
    PGPSecretKey sec2 = new PGPSecretKey(prv2, pub2, null, true, null);
    // use pub2.getEncoded() and >>sec2.getEncoded()<<

As Charlie Brown would say, bleah!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM