Java – how to use jruby9 1.2. Use GPG to encrypt files in 0?

I am using GPG to encrypt files and send them to my j Ruby project But I didn't find enough resources I've tried ruby gpgme, but jruby doesn't support C library I tried to read bouncy castle, but I was overwhelmed by class documents and couldn't find a simple article for encrypting files

Vivek's answer in this year is close to my solution, but the solution is only the solution I'm currently focusing on this article and trying to invalidate the Java code interface in jruby I think the EncryptFile function is what I need, as shown below:

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();
    }

)

I got the following error:

NoMethodError: undefined method `ZIP' for Java::OrgBouncycastleOpenpgp::PGPCompressedData:Class

stay

PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);

If you can use GPG in jruby as a whole through code or encrypted files, it will be a great help

Update 1. The zip value is expressed as a constant of integer value and listed on this page

Update 2 I did the following:

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

I have a public key generated from OS How can I create a pgppublic key instance enckey from a public key string?

Solution

I can't find enough answers or gems to do, including the PGP Library in the project folder So I have assigned this repo to this repo to rails and the GPG Library of the system It applies to Ubuntu I haven't tested it on other machines

Encryption:

The public key is installed on the machine

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

decrypt

In a machine with a private key

decryptObj = Gpgr::Decrypt::GpgFileForDecryption.new
decryptObj.file = <path_to_file_to_decrypt>
decryptObj.file_output = <path_to_output_file>
decryptObj.decrypt
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>