如何在jruby9.1.2.0中使用PGP加密来加密文件?

13

我正在尝试在我的jruby项目中使用gpg加密来加密文件,但是我没有找到足够的资源。我尝试使用ruby-gpgme,但是jruby不支持C库。我试着阅读Bouncy Castle,但是我被类文档压倒了,没有找到一个简单的文章来对文件进行加密。

Vivek在这个问题的回答接近了我的解决方案,但只有解密文件的方法。我目前正在按照这篇文章的指导,试图将Java代码与jruby接口起来,但一直没有成功。我认为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);

如果您能帮我编写代码或使用gpg在jruby中加密文件,那将是非常有帮助的。

更新1 ZIP值原来是一个整数常量,并列在这个页面中。

更新2 我已经完成了这个函数:

PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(dataEncryptor);
    encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(encKey)); // encKey is class PGPPublicKey's instance

我有从操作系统生成的公钥。 我应该如何从我拥有的公钥字符串创建一个PGPPublic Key实例 encKey ?


ZIP 可能是一个类常量而不是一个方法。尝试使用 PGPCompressedData::ZIP 来引用一个类常量,而不是使用 .ZIP 来引用一个类方法。 - EnabrenTane
是的,我从这个页面找到了zip的相应常量值。https://www.bouncycastle.org/docs/pgdocs1.5on/constant-values.html#org.bouncycastle.openpgp.PGPLiteralData.BINARY - Pravin
我仍在尝试编写一个包装器来进行加密。真的需要帮助。 - Pravin
是的,在Ruby中,PGPCompressedData.zip会调用一个方法。你想引用类内部的常量。使用::而不是.应该可以解决问题。通过PGPCompressedData.constants.sortPGPCompressedData.methods.sort来反思PGPCompressedData也可能有所帮助。如果它是一个常量,将PGPCompressedData.ZIP替换为PGPCompressedData::ZIP就可以了。 - EnabrenTane
@EnabrenTane 我刚刚传递了整数值。 - Pravin
1个回答

0

我找不到足够的答案或宝石来完成任务,包括pgp库在项目文件夹中。所以我已经fork了this repothis repo,以便在rails和系统的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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接