gpt4 book ai didi

java - 使用 PGP 公钥 block 的实例 PGPPublicKey

转载 作者:行者123 更新时间:2023-12-02 13:00:36 24 4
gpt4 key购买 nike

我已获得一个 PGP 公钥 block ,我可以用它来加密 csv 文件。使用 BouncyCaSTLe 库,这是我正在使用的方法:

public static void encryptFile(
OutputStream out,
String fileName,
PGPPublicKey encKey,
boolean armor,
boolean withIntegrityCheck)
throws IOException, NoSuchProviderException, PGPException {
Security.addProvider(new BouncyCastleProvider());

if (armor) {
out = new ArmoredOutputStream(out);
}

ByteArrayOutputStream bOut = new ByteArrayOutputStream();
PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);

PGPUtil.writeFileToLiteralData(
comData.open(bOut),
PGPLiteralData.BINARY,
new File(fileName));

comData.close();

BcPGPDataEncryptorBuilder dataEncryptor = new BcPGPDataEncryptorBuilder(PGPEncryptedData.TRIPLE_DES);
dataEncryptor.setWithIntegrityPacket(withIntegrityCheck);
dataEncryptor.setSecureRandom(new SecureRandom());

PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(dataEncryptor);
encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(encKey));

byte[] bytes = bOut.toByteArray();
OutputStream cOut = encryptedDataGenerator.open(out, bytes.length);
cOut.write(bytes);
cOut.close();
out.close();
}

当涉及 PGPPublicKey 时,我不太确定如何向此方法提供参数。如何仅在给定我的 Key block 的情况下实例化该对象?

最佳答案

将您的 key 文件(假设您有 key 作为文件)传递给此方法,它将返回 PGPPublicKey

  /** The fingerprint calculator to use whenever it is needed. */ 
static final KeyFingerPrintCalculator FP_CALC = new BcKeyFingerprintCalculator();

// Private class method readPublicKeyFromCol
private static PGPPublicKey readPublicKeyFromCol(InputStream in)
throws Exception {
PGPPublicKeyRing pkRing = null;
PGPPublicKeyRingCollection pkCol = new PGPPublicKeyRingCollection(in, FP_CALC);
System.out.println("key ring size=" + pkCol.size());
Iterator it = pkCol.getKeyRings();
while (it.hasNext()) {
pkRing = (PGPPublicKeyRing) it.next();
Iterator pkIt = pkRing.getPublicKeys();
while (pkIt.hasNext()) {
PGPPublicKey key = (PGPPublicKey) pkIt.next();
System.out.println("Encryption key = " + key.isEncryptionKey() + ", Master key = " +
key.isMasterKey());
if (key.isEncryptionKey())
return key;
}
}
return null;
}

!!!代码复制自 sample code

关于java - 使用 PGP 公钥 block 的实例 PGPPublicKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44314400/

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