gpt4 book ai didi

java - 通过 Bouncy CaSTLe 提取 GPG key 使用标志

转载 作者:行者123 更新时间:2023-11-30 07:05:33 24 4
gpt4 key购买 nike

虽然 PGPPublicKey 类提供了一个 isEncryptionKey() 方法来确定公钥的算法是否可以用于加密目的(RSA_GENERAL, RSA_ENCRYPT, ELGAMAL_GENERAL, ELGAMAL_ENCRYPT) 仅此不足以选择有效的加密子 key 。

如 GnuPG packet.h 中所示,有关于存储在数据包中的公钥的预期用途的信息:

 41 /* Usage flags */
42 #define PUBKEY_USAGE_SIG GCRY_PK_USAGE_SIGN /* Good for signatures. */
43 #define PUBKEY_USAGE_ENC GCRY_PK_USAGE_ENCR /* Good for encryption. */
44 #define PUBKEY_USAGE_CERT GCRY_PK_USAGE_CERT /* Also good to certify keys.*/
45 #define PUBKEY_USAGE_AUTH GCRY_PK_USAGE_AUTH /* Good for authentication. */
46 #define PUBKEY_USAGE_UNKNOWN GCRY_PK_USAGE_UNKN /* Unknown usage flag. */
47 #define PUBKEY_USAGE_NONE 256 /* No usage given. */

我的问题是,鉴于 Bouncy CaSTLe 不公开这些标志,从 Java 中的 PublicKeyPacket 中提取此 key 使用信息的建议方法是什么?

最佳答案

我想通了。对于后代,这里是解决方案:

// If Key Usage flags are present, we must respect them:
int keyFlagsEncountered = 0;
boolean keyUsageAllowsEncryption = false;

Iterator<PGPSignature> i = key.getSignatures();
while(i.hasNext()) {
PGPSignature signature = i.next();
int keyFlags = signature.getHashedSubPackets().getKeyFlags();
keyFlagsEncountered += keyFlags;

boolean isEncryptComms = (keyFlags & KeyFlags.ENCRYPT_COMMS) > 0;
boolean isEncryptStorage = (keyFlags & KeyFlags.ENCRYPT_STORAGE) > 0;
// Other KeyFlags available here (AUTHENTICATION, SIGN_DATA, CERTIFY_OTHER).

if (isEncryptComms || isEncryptStorage) {
keyUsageAllowsEncryption = true;
}
}

// However, if Key Usage flags are not present (older key, or key generation process simply did not include the flags)
// then we still attempt to use an encryption key using the existing methods:
keyUsageAllowsEncryption = keyFlagsEncountered == 0 || keyUsageAllowsEncryption;

关于java - 通过 Bouncy CaSTLe 提取 GPG key 使用标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26554918/

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