【发布时间】:2017-04-09 15:32:05
【问题描述】:
我正在尝试在将文件发送到我的 jruby 项目之前使用 gpg 加密对文件进行加密。但是我没有找到足够的资源。我尝试使用ruby-gpgme,但 jruby 不支持 C 库。我尝试阅读Bouncy Castle,但我被类文档所淹没,并没有找到加密文件的简单文章。
Vivek 在this 问题中的回答接近我的解决方案,但只有解密文件的解决方案。我目前正在关注this article 并尝试在 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 值原来是整数值的常量,并列在this 页面中。
更新 2 我做到了:
PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(dataEncryptor);
encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(encKey)); // encKey is class PGPPublicKey's instance
我有从操作系统生成的公钥。如何从我拥有的公钥字符串创建 PGPPublic Key 实例encKey?
【问题讨论】:
-
ZIP可能是类常量而不是方法。尝试使用PGPCompressedData::ZIP来引用类常量而不是.ZIP来引用类方法。 -
是的,我从这个页面找到了相应的 zip 常量值。 bouncycastle.org/docs/pgdocs1.5on/…
-
我仍在尝试围绕它编写一个包装器,只是为了加密。非常感谢您的帮助。
-
是的,在 Ruby 中
PGPCompressedData.zip会调用一个方法。您想在类中引用一个常量。使用::而不是.应该可以解决问题。用PGPCompressedData.constants.sort和PGPCompressedData.methods.sort反思PGPCompressedData也可能有所帮助。如果PGPCompressedData.ZIP是常量,则将PGPCompressedData.ZIP替换为PGPCompressedData::ZIP应该这样做。 -
@EnabrenTane 我刚刚传递了整数值。
标签: java ruby-on-rails encryption bouncycastle openpgp