当前位置: 首页 > 知识库问答 >
问题:

Java中如何用Bouncy Castle对公共PGP密钥进行签名

锺功
2023-03-14

我正在编写PGP服务器为用户生成密钥。
我想用Bouncy Castle在Java中用我的私钥签署任何生成的PGP公钥,使其为我的用户所信任。

我没有问题生成密钥对-它工作完美。

gpg --check-sigs "Adrian (test) <al@p-c.pl>"
pub   4096R/9D5D4AB8 2018-06-20
sig-         E9798A8A 2018-08-04  Test (test) <test@sample.com>
uid                  Adrian (test) <al@p-c.pl>
sig!         9D5D4AB8 2018-06-20  Adrian (test) <al@p-c.pl>

1 incorrect signature
public void signKey(String id, PGPSecretKey mySecretKey, PGPPublicKey publicKeyToBeSigned, char[] passPhrase, OutputStream out) throws PGPException, IOException {

    PGPPrivateKey pgpPrivKey = mySecretKey.extractPrivateKey(
            new JcePBESecretKeyDecryptorBuilder().setProvider(provider).build(passPhrase));

    PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(
            new JcaPGPContentSignerBuilder(mySecretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1));

    signatureGenerator.init(PGPSignature.DIRECT_KEY, pgpPrivKey);

    PGPSignature signature = signatureGenerator.generateCertification(id, publicKeyToBeSigned);

    PGPPublicKey result = PGPPublicKey.addCertification(publicKeyToBeSigned, signature);

    out = new ArmoredOutputStream(out);

    result.encode(out);
    out.close();
}
public void signPublicKey(PGPSecretKey secretKey, String secretKeyPass, PGPPublicKey keyToBeSigned, String notationName, String notationValue, OutputStream out) throws Exception {

    PGPPrivateKey pgpPrivKey = secretKey.extractPrivateKey(
            new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(secretKeyPass.toCharArray()));

    PGPSignatureGenerator sGen = new PGPSignatureGenerator(
            new JcaPGPContentSignerBuilder(secretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1).setProvider("BC"));

    sGen.init(PGPSignature.DIRECT_KEY, pgpPrivKey);

    PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();

    boolean isHumanReadable = true;

    spGen.setNotationData(true, isHumanReadable, notationName, notationValue);

    PGPSignatureSubpacketVector packetVector = spGen.generate();

    sGen.setHashedSubpackets(packetVector);

    PGPPublicKey result = PGPPublicKey.addCertification(keyToBeSigned, sGen.generate());

    out = new ArmoredOutputStream(out);

    result.encode(out);
    out.close();
}
@Test
public void signKey() throws Exception {

    FileInputStream in = new FileInputStream("src/test/resources/secret.dat");

    PgpServiceImpl pgp = new PgpServiceImpl();

    PGPSecretKey pgpSecretKey = pgp.readSecretKey(in, "..........".toCharArray());
    PGPPublicKey pubKey = pgp.readPublicKey(new FileInputStream("src/test/resources/pub_other.dat"));

    ByteArrayOutputStream res = new ByteArrayOutputStream();

    pgp.signPublicKey(pgpSecretKey, "..........", pubKey, "Test (test) <test@sample.com>", "Adrian (test) <al@p-c.pl>", res);

    System.out.println(new String(res.toByteArray()));
}
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: BCPG v1.60

mQINBFsqb5YBEADHhGX7m27h2gyj1sXtVBy6VmTzxf4uI9MhLNK1fEuM4EFHwS9A
7vIUZdG3rWnCEZkLiRChSFoDaYIbLfph7vR+PQq/dvIxefkX9CDIVkMeHgQI/Kfn
1dtkSTtV8eHogdKhii2bDFNDY01WgIyxQtdgs+QrKXexVWbxBwBS3wUorUaegvfQ
b55w4LtjMfcSqQanIOMb9G0yVAx9/RxCHBSGLEVzdQroTQQKyX0EBPFibB5Mxeaf
MHm821ds3a3rE/FCCCZRgRnGVTfgBgU1/WtAoY4MsJ6MEVJjGDkT0VkVPtE+8VE/
M2wRQx8sR42MhidDT9Kgmb2ma6sIOLdxnCbwlegnttpTG5zlSL98jxVtalvXvylO
hXLunrtJPQC1q2icjMwu4nIGaWNxT1bk7JAG29x80qE2NB9Ms8+hYlsRPn6s5bY5
TGI4fzXY7/wfn+Ho26nu69YTdpPuRfI7EJi0uOvsLeRCmszFzaIyFx1Ebt8qEDwB
c+WKcBwWb80tWUcARALdZAYGeMlJN4zELpQhFWb1bCmHKJfPUqr2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S+XUT6dz1pTH3hcQ5KZiw78XbZxUALFDqm3qKmjZnN4SUpbA71kK0
sg+RAL8ydjQerQ+Lu4+6JOK5qDqsM/xKkpNRcgAL9g8GC5w1B/7pSh3Im+02U/Aa
g7gi5sYtqo8fTZspHMf5pIeT75ln3IBLmkNSJUthNNixNZWXgn83L40twnPKaaQC
0QNua0xZJubFzmgg5KempIIGdYOgQh8rNc28cCsFrrIg/ggsifsVJDzVtMnSBBYb
L8G2Eb+fUiqCOL1jNEFy/aCBzMLWEXkBmdl9nwZQ1eB9qEaBRpfTLuIiq8U1KA8/
fOfYtuftsUziuGYj9CvN//esA75HxwVtL7ssn1TGdWMYQ9KhuGmGHLkKBRUT91hU
HtO0SXQ8tyWkICJ3Lp9irEF/tdFwPorlLYoch3Ya5ybySXMtHprcBN5eCfBZpFw1
AllidwK0nvDQeaA0ZHVvMyXATO0AhCY6WArORaHM6tER51qxgz9vpffYaFxwJfn4
9dApqV4Xx7Ei1VEdQ7t0YGcdvNByLRDeEvwEhC7808jPCLGoIP4rOKyAk3gTU98w
0vg3h9OLkCueT5482/v5DFbtzPNOjksIbMhmNt0KUOxfla+S48Zb0xSw0jXZEDOi
snGdpaUOpLJZRe2zI/i59UIHi1OoKERxYw==
=9lxJ
-----END PGP PUBLIC KEY BLOCK-----

共有1个答案

诸葛柏
2023-03-14

这非常有效:

public void signPublicKey(PGPSecretKey secretKey, char[] passPhrase, PGPPublicKey keyToBeSigned, OutputStream out) throws PGPException, IOException {

        PGPPrivateKey pgpPrivKey = secretKey.extractPrivateKey(
                new JcePBESecretKeyDecryptorBuilder().setProvider( provider )
                        .build(passPhrase));

        PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(
                new JcaPGPContentSignerBuilder( secretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1 ) );

        signatureGenerator.init( PGPSignature.DEFAULT_CERTIFICATION, pgpPrivKey );

        Iterator<String> ids = keyToBeSigned.getUserIDs();
        if(!ids.hasNext()) throw new IllegalArgumentException("klucz nie posiada żadnego User ID");
        String id = ids.next();

        PGPSignature signature = signatureGenerator.generateCertification(id, keyToBeSigned);
        PGPPublicKey newKey = PGPPublicKey.addCertification(keyToBeSigned, id, signature);

        out = new ArmoredOutputStream(out);

        newKey.encode(out);
        out.close();
    }
 类似资料:
  • 在SSO的实现中,我使用了bouncycastle(JAVA)进行签名、加密、解密和签名的验证。我有原始PGP公钥和私钥,我需要将它们存储在Java Keystore中。这些PGP公钥没有证书。 我知道对于公钥(根据Keystore的javadoc:http://docs.oracle.com/javase/6/docs/api/java/security/keystore.html),我必须创建

  • 我正在尝试编写一个加密文件,它将使用gpg解密,并将以增量方式写入行,而不是在一个块中。我已经在GnuPG中生成了密钥,并且正在使用公钥加密。下面是我正在使用的加密方法: 我有一个小的原型测试类来使用这种方法: 我怎样才能修改这段代码,使它既能加密两行,又能让GnuPG解密它们呢?

  • 我正在尝试在BouncyCastle中导入公共和私有ECDH密钥。为了导入公钥,我使用了下面的C#代码,代码运行良好: 公钥:042E3E5CCF6B9AB04BE7A22F3FACCDE73C87E87155394A34815408A896CA18A374DAC669AF36220FC863767F4AF47507C5BC221FC4A19874DAF39B074E3EB8 私钥:be3f9bf

  • 我目前正在用java编写一个加密消息传递服务,我使用的是bouncycastle PGP库。我编写了一个生成密钥对的测试程序,并对消息进行加密/解密。这已经工作了一段时间,但它最近在解密阶段停止了,给了我一个InvalidKeyException。 我做了一些研究,下载了JCE.jar文件,并将它们导入到我的项目中(通过Eclipse Project->Properties->add extern

  • 我想使用BouncyCastle解析和验证OpenPGP分离签名。签名如下所示: 下面是我如何尝试在Kotlin中创建CMSSignedData: 我应该如何使用BouncyCastle验证这种签名?