加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何在jruby9.1.2.0中使用gpg加密加密文件?

发布时间:2020-12-14 16:20:10 所属栏目:Java 来源:网络整理
导读:我正在使用gpg加密加密文件,然后将其发送到我的j ruby项目中.但是我没有找到足够的资源.我试过使用 ruby-gpgme,但jruby不支持C库.我试着阅读 Bouncy Castle,但是我被类文档压倒了,没有找到一个用于加密文件的简单文章. Vivek在this年的回答接近我的解决方案,
我正在使用gpg加密加密文件,然后将其发送到我的j ruby项目中.但是我没有找到足够的资源.我试过使用 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

我有从OS生成的公钥.我如何从公钥字符串创建一个PGPPublic Key实例encKey?

解决方法

我找不到足够的答案或宝石来做,包括项目文件夹中的pgp库.所以我已经把 this repo到 this 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读