[英]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.