gpt4 book ai didi

java 使用 bouncycaSTLe 签署公共(public) pgp key

转载 作者:行者123 更新时间:2023-11-30 08:15:57 28 4
gpt4 key购买 nike

我有一个疑问...据说我必须使用 bouncycaSTLe api 签署一个 pgp 公钥。现在:据我了解,用另一种方法签署 key 最终会向该公钥添加一个“证书”。因此没有任何其他方法,我在图书馆盲目搜索。到目前为止,我唯一的发现是 PGPSignatureGenerator 中的 generateCertification 方法。但是这种方法会在主 PgpPublicKey 和另一个 PgpPublicKey 之间生成证书。这让我觉得很奇怪:我假设为了信任另一个公钥,必须用你自己的私有(private) pgp key 签名,就像在常规 x.509 中以某种方式进行 CA 认证一样。这是我在尝试从其他库中获得一些想法时看到的一些方法的假设:例如,didisoft 在 keystore 上有一个类似的方法,您必须在其中提供 PgpPrivatekey keyuid ...

有人有任何提示或一段代码可以提出吗?提前致谢。

最佳答案

这可用于检查一个 key 是否为另一个 key 提供了默认证书

  /**
* Signs a public key
*
* @param publicKeyRing a public key ring containing the single public key to sign
* @param id the id we are certifying against the public key
* @param secretKey the signing key
* @param secretKeyPassword the signing key password
*
* @return a public key ring with the signed public key
*/
public static PGPPublicKeyRing signPublicKey( PGPPublicKeyRing publicKeyRing, String id, PGPSecretKey secretKey,
String secretKeyPassword ) throws PGPException
{
try
{
PGPPublicKey oldKey = publicKeyRing.getPublicKey();

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

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

signatureGenerator.init( PGPSignature.DEFAULT_CERTIFICATION, pgpPrivKey );

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

PGPPublicKey newKey = PGPPublicKey.addCertification( oldKey, signature );

PGPPublicKeyRing newPublicKeyRing = PGPPublicKeyRing.removePublicKey( publicKeyRing, oldKey );

return PGPPublicKeyRing.insertPublicKey( newPublicKeyRing, newKey );
}
catch ( Exception e )
{
//throw custom exception
throw new PGPException( "Error signing public key", e );
}
}


/**
* Verifies that a public key is signed with another public key
*
* @param keyToVerify the public key to verify
* @param id the id we are verifying against the public key
* @param keyToVerifyWith the key to verify with
*
* @return true if verified, false otherwise
*/
public static boolean verifyPublicKey( PGPPublicKey keyToVerify, String id, PGPPublicKey keyToVerifyWith )
throws PGPException
{
try
{
Iterator<PGPSignature> signIterator = keyToVerify.getSignatures();
while ( signIterator.hasNext() )
{
PGPSignature signature = signIterator.next();
signature.init( new JcaPGPContentVerifierBuilderProvider().setProvider( provider ), keyToVerifyWith );
if ( signature.verifyCertification( id.getBytes(), keyToVerify ) )
{
return true;
}
}
return false;
}
catch ( Exception e )
{
//throw custom exception
throw new PGPException( "Error verifying public key", e );
}
}

关于java 使用 bouncycaSTLe 签署公共(public) pgp key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28591684/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com