gpt4 book ai didi

java - 如何使用 BouncyCaSTLe 生成没有密码的 OpenPGP KeyPair?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:17:08 26 4
gpt4 key购买 nike

我正在从事一个需要为公钥加密生成 GPG key 的项目。我选择的语言是 Scala,加密库是 BouncyCastle

key 生成工作正常,但我找不到生成 key 的方法没有密码。在 BounceyCaSTLe 中生成 key 的每个类和每个方法都需要一个密码。

使用空密码会导致 gpg 在解密/签名时仍然要求我提供它,传递 null 会在 key 生成时引发异常。

这是我正在使用的代码(去除了导入):

object KeyGenerator {
Security.addProvider(new BouncyCastleProvider())

val kpg = KeyPairGenerator.getInstance("RSA", "BC")
kpg.initialize(2048)

def generateKeyPair(userID: String,
expiration: Option[Date]): PGPSecretKeyRing = {


val now = new Date

val keyPair = kpg.generateKeyPair();
val secretKey = new PGPKeyPair(RSA_GENERAL,
keyPair,
now)

val keyPair2 = kpg.generateKeyPair();
val secretKey2 = new PGPKeyPair(RSA_GENERAL,
keyPair2,
now)

val subpacketGen = new PGPSignatureSubpacketGenerator
subpacketGen.setKeyFlags(true, KeyFlags.CERTIFY_OTHER | KeyFlags.SIGN_DATA
| KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE)


val keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION,
secretKey,
userID,
RSA_GENERAL,
"".toCharArray,
true,
subpacketGen.generate,
null,
new SecureRandom(),
"BC");
keyRingGen.addSubKey(secretKey2)
keyRingGen.generateSecretKeyRing
}
}

最佳答案

您可以尝试对 PGPKeyRingGenerator 构造函数的 encAlgorithm 参数使用 SymmetricKeyAlgorithmTags.NULL(即不加密)。 API 可能仍然需要占位符“passPhrase”,但我相信在这种情况下它会被忽略。

关于java - 如何使用 BouncyCaSTLe 生成没有密码的 OpenPGP KeyPair?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5607698/

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