是否有适用于 Java/Scala 的好的 GnuPG 加密库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1455037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:27:07  来源:igfitidea点击:

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

javascalajvmgnupg

提问by James Shade

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.

我希望能够使用 Java 应用程序中的 GnuPG 加密磁盘上的文件和/或内存中的数据。如果可能,我想避免对 GPG 命令行工具进行系统调用。

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

是否有推荐的库,或者您能否推荐从 Java(或 Scala)进行 GPG 加密的最佳方法?

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

我正在开发并打算在 Linux 环境中运行该应用程序,尽管首选跨平台解决方案。

采纳答案by VonC

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

您可以尝试调用BouncyCastle.org的 JAVA API 。

Its documentationmentions:

它的文档提到:

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

Bouncy Castle Crypto 包是加密算法的 Java 实现。

You have here an example of openpgp ByteArrayHandler.

你在这里有一个 openpgp ByteArrayHandler 的例子

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

不过,BouncyCastle 加密和 GnuGP 加密之间可能存在一些不兼容,因为 BouncyCastle 不使用 GnuPG,而是在 Java 中实现 OpenPGP (RFC2440)。

回答by Vivek Kumar

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

我最近不得不研究 GPG 加密解密,并且确实发现 BountyCastle 的 PGP 库可以解决问题。步骤是

1) Add the version in pom.xml properties

1) 在 pom.xml 属性中添加版本

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

2) Add the following dependencies

2)添加以下依赖

        <!-- 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

3) 在实现类中添加了 Java Security 的提供者

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

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

4) 其余代码只是简单的 Java 实现

    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.

我希望这有帮助。

回答by Hans-Christoph Steiner

There is https://github.com/smartrevolution/gnupg-for-javawhich 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/smartrevolution/gnupg-for-java它基于 gpgme,并在 GnuPG 1.4 之上工作。我们正在为 GnuPG 2.x 更新它,并在我们的 Android 应用程序中使用它。您可以在此处获取代码: