Is there a good GnuPG encryption library for Java/Scala? [closed]

南楼画角 提交于 2019-12-18 10:13:49

问题


I would like to be able to encrypt files on disk and/or data in memory using GnuPG from a Java application. If possible I'd like to avoid having to make system calls out to the GPG command line tools.

Is there a recommended library, or can you recommend the best approach to GPG encrypting from Java (or Scala)?

I'm developing and intend to run the application in a Linux environment, although a cross-platform solution would be preferred.


回答1:


You can try to call the JAVA API of BouncyCastle.org.

Its documentation mentions:

The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms.

You have here an example of openpgp ByteArrayHandler.

There might be some incompatibility between BouncyCastle encryption and GnuGP encryption though, since BouncyCastle does not use GnuPG, but rather implements OpenPGP (RFC2440) in Java.




回答2:


I recently had to work on GPG encryption-decryption and did find BountyCastle's PGP library does the trick. The steps were

1) Add the version in pom.xml properties

        <org.bouncycastle.version>1.46</org.bouncycastle.version>

2) Add the following dependencies

        <!-- Dependency for PGP and GPG Encryption-Decryption -->
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcmail-jdk15</artifactId>
            <version>${org.bouncycastle.version}</version>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcpg-jdk15</artifactId>
            <version>${org.bouncycastle.version}</version>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15</artifactId>
            <version>${org.bouncycastle.version}</version>
        </dependency>

3) In the implementation class added the provider with Java Security

         Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

4) The rest of the code was just simple Java implementation

    File encryptedFile = new File(encryptedFileName);
    byte[]  encryptedByteArray = FileUtils.readFileToByteArray(inputFile);      
    byte[] decryptedByteArray = ByteArrayHandler.decrypt(encryptedByteArray, passPhrase.toCharArray());
    String decryptedString = new String(decryptedByteArray);

I hope this helps.




回答3:


There is https://github.com/smartrevolution/gnupg-for-java which is based on gpgme, and works on top of GnuPG 1.4. We're updating it for GnuPG 2.x and are using it in our Android app. You can get the code to those here:

  • https://github.com/guardianproject/gnupg-for-java
  • https://github.com/guardianproject/gnupg-for-android


来源:https://stackoverflow.com/questions/1455037/is-there-a-good-gnupg-encryption-library-for-java-scala

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!