[英]How to encrypt a file using PGP encryption in jruby9.1.2.0?
我正在嘗試在將文件發送到我的 jruby 項目之前使用 gpg 加密對其進行加密。 但是我沒有找到足夠的資源。 我嘗試使用ruby-gpgme但 jruby 不支持 C 庫。 我嘗試閱讀Bouncy Castle,但我被課程文檔所淹沒,並且沒有找到有關加密文件的簡單文章。
Vivek 在這個問題中的回答接近我的解決方案,但只有解密文件的解決方案。 我目前正在關注這篇文章,並試圖在 jruby 中連接 java 代碼,但無濟於事。 我認為encryptFile
函數是我需要的,如下所示:
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();
}
)
我收到以下錯誤:
NoMethodError: undefined method `ZIP' for Java::OrgBouncycastleOpenpgp::PGPCompressedData:Class
在
PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
如果您能在整個代碼或使用 jruby 中的 gpg 加密文件方面為我提供幫助,那將是一個很大的幫助。
更新 1 ZIP 值原來是整數值的常量,並在此頁面中列出。
更新 2我做到了,直到功能:
PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(dataEncryptor);
encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(encKey)); // encKey is class PGPPublicKey's instance
我有從操作系統生成的公鑰。 如何從我擁有的公鑰字符串創建 PGPPublic Key 實例encKey
?
我找不到足夠的答案或 gem 來完成它,包括項目文件夾中的 pgp 庫。 因此,我已付出這個回購協議,以該回購到接口軌和系統的GPG庫。 它適用於 ubuntu。 我沒有在其他機器上測試過。
在安裝了公鑰的機器上
encryptObj = Gpgr::Encrypt::GpgFileForEncryption.new
encryptObj.email_address = <email_of_gpg_owner>
encryptObj.file = <path_to_file_to_encrypt>
encryptObj.file_output = <path_to_output_file>
encryptObj.encrypt
在有私鑰的機器上
decryptObj = Gpgr::Decrypt::GpgFileForDecryption.new
decryptObj.file = <path_to_file_to_decrypt>
decryptObj.file_output = <path_to_output_file>
decryptObj.decrypt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.