0
我得到了一個PGP公鑰密碼塊,我應該用它加密一個csv文件。使用BouncyCastle的庫,這是我使用的方法:我不太肯定我怎麼能提供的參數這種方法,當談到PGPPublicKey
使用PGP公開密鑰塊的實例PGPPublicKey
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();
}
。我怎樣才能實例化這個對象只給我的密鑰塊?
感謝複製!讓我測試並回到你身邊。 – Disasterkid
肯定!另外檢查示例代碼鏈接 – Bhavesh
我認爲'PGPPublicKeyRingCollection'對象除了'InputStream'外還需要'KeyFingerPrintCalculator'。 – Disasterkid