0
我已經得到我的代碼,可以加載公鑰,並使用其他人提供的公鑰加密byte []數組。但是,當我解密文件時,它沒有.csv擴展名。我可以在Excel中打開它,一旦我打開它,它看起來就像是一個csv,但該文件沒有任何關聯擴展名,所以我必須在Excel中奇怪地打開它。BouncyCastle openPGP加密byte []數組作爲csv文件
我也附上了我下面的代碼,我只是無法弄清楚哪兒來那麼,當我使用克列奧帕特拉解密,它顯示了一個.csv擴展名修改它。我試圖將輸出流從.gpg文件更改爲.csv,但隨後會引發錯誤,指出它無法確定這是否是OpenPGP簽名。我想我必須對lData.open()方法做些什麼,但我可以放在那裏的唯一選項是PGPLiteralData.TEXT,BINARY和UTF8。也許這與添加簽名到文件有關?
順便說一句,對於下面的代碼,公鑰已經加載到一個名爲pubkey的變量中。
byte[] bytesArray = getAccessChannels(customerAlias);
OutputStream out = new FileOutputStream("/path/to/file/eData.gpg");
Security.addProvider(new BouncyCastleProvider());
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedDataGenerator.ZIP);
OutputStream cos = comData.open(bOut); // open it with the final destination
PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
OutputStream pOut = lData.open(cos, PGPLiteralData.TEXT, "Report.csv", bytesArray.length, new Date());
pOut.write(bytesArray);
lData.close();
comData.close();
PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.AES_256).setSecureRandom(new SecureRandom()));
cPk.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(pubKey));
byte[] bytes = bOut.toByteArray();
OutputStream cOut = cPk.open(out, bytes.length);
cOut.write(bytes); // obtain the actual bytes from the compressed stream
cOut.close();