Java/Scala 有没有好的 GnuPG 加密库?

发布于 2024-08-05 19:54:02 字数 1539 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千と千尋 2024-08-12 19:54:02

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

文档提到:

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

你在这里openpgp ByteArrayHandler 的示例

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

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.

不念旧人 2024-08-12 19:54:02

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

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

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

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) 在实现类中添加具有 Java 安全性的提供程序

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

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

巨坚强 2024-08-12 19:54:02

https://github.com/smartrevolution/gnupg-for-java 这是基于 gpgme,并在 GnuPG 1.4 之上工作。我们正在将其更新为 GnuPG 2.x 并在我们的 Android 应用程序中使用它。您可以在此处获取代码:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文