bouncycastle 提供程序找不到算法所需的类

发布于 2024-11-27 13:52:35 字数 1730 浏览 1 评论 0原文

我正在尝试使用 bouncycastle 使用公钥加密文件。 我已经以编程方式注册了提供程序:

Security.addProvider(new BouncyCastleProvider());

我成功创建了公钥对象。

当我使用 PGPEncryptedDataGenerator 和密钥加密文件时,我收到 ClassNotFound 异常。

看来提供者无法在运行时找到这个类,尽管我确信我有它的 jar...

我正在 tomcat 上运行我的应用程序。 使用 Maven 处理依赖关系 - 我放置的 bouncy castle jar 是 bcpg、bcprov、bcmail、bctsp。 我尝试使用 1.4 和 1.6 版本,但没有成功。 我在 eclipse 的 Maven 插件中使用了“依赖层次结构”,并在 pom 中排除了这些内容,以确保我的项目中没有多个版本的 bouncycastle。

这是堆栈跟踪:

org.bouncycastle.openpgp.PGPException: exception encrypting session key
        at org.bouncycastle.openpgp.PGPEncryptedDataGenerator.open(Unknown Source)
        at org.bouncycastle.openpgp.PGPEncryptedDataGenerator.open(Unknown Source)
.....(web application stack trace and uninteresting stuff).....
Caused by: java.security.NoSuchAlgorithmException: No such algorithm: ElGamal/ECB/PKCS1Padding
        at javax.crypto.Cipher.getInstance(DashoA13*..)
        at org.bouncycastle.openpgp.PGPEncryptedDataGenerator$PubMethod.addSessionInfo(Unknown Source)
        ... 42 more
Caused by: java.security.NoSuchAlgorithmException: class configured for Cipher(provider: BC)cannot be found.
        at java.security.Provider$Service.getImplClass(Provider.java:1268)
        at java.security.Provider$Service.newInstance(Provider.java:1220)
        ... 44 more
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.jce.provider.JCEElGamalCipher$NoPadding
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
        at java.security.Provider$Service.getImplClass(Provider.java:1262)

I'm trying to use bouncycastle to encrypt a file using a public key.
I've registered the provider programatically:

Security.addProvider(new BouncyCastleProvider());

I created the public key object successfully.

when i get to encrypting the file using a PGPEncryptedDataGenerator and the key I get a ClassNotFound exception.

It seems the provider can't find this class at runtime, though I know for sure I have its jar...

I'm running my app on tomcat.
Using maven to handle dependencies - the bouncy castle jars I put are bcpg, bcprov, bcmail, bctsp.
I tried using both the 1.4 and the 1.6 versions without success.
I used the "dependency hierarchy" in maven plugin for eclipse and exclusions in the pom to make sure that there are no multiple versions of bouncycastle in my project.

This is the stack trace:

org.bouncycastle.openpgp.PGPException: exception encrypting session key
        at org.bouncycastle.openpgp.PGPEncryptedDataGenerator.open(Unknown Source)
        at org.bouncycastle.openpgp.PGPEncryptedDataGenerator.open(Unknown Source)
.....(web application stack trace and uninteresting stuff).....
Caused by: java.security.NoSuchAlgorithmException: No such algorithm: ElGamal/ECB/PKCS1Padding
        at javax.crypto.Cipher.getInstance(DashoA13*..)
        at org.bouncycastle.openpgp.PGPEncryptedDataGenerator$PubMethod.addSessionInfo(Unknown Source)
        ... 42 more
Caused by: java.security.NoSuchAlgorithmException: class configured for Cipher(provider: BC)cannot be found.
        at java.security.Provider$Service.getImplClass(Provider.java:1268)
        at java.security.Provider$Service.newInstance(Provider.java:1220)
        ... 44 more
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.jce.provider.JCEElGamalCipher$NoPadding
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
        at java.security.Provider$Service.getImplClass(Provider.java:1262)

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

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

发布评论

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

评论(3

挽清梦 2024-12-04 13:52:35

您遇到 BouncyCastle 安全提供程序安装问题,您需要将

  • BouncyCastle 添加到 JRE/JDK $JAVA_HOME/jre/lib/security/java.security 文件作为提供程序(请确保将其添加运行时使用的 JRE,例如,如果您安装了多个 JRE/JDK)

security.provider.2=org.bouncycastle.jce.provider.BouncyCastleProvider

(并重新编号其下方的安全提供程序 - 不要将其作为最高优先级的提供程序)。

  • 或者您可以以编程方式添加 BouncyCastle,就像您在上面尝试执行的那样,但在这种情况下,安全策略 $JAVA_HOME/jre/lib/security/java.policy 应该是“无限制”(您可能可以从 Java 主页下载无限制的策略文件)。

You have a BouncyCastle Security provider installation problem, you need to either

  • Add BouncyCastle to the JRE/JDK $JAVA_HOME/jre/lib/security/java.security file as a provider (be sure that you add it to the JRE you use when running, eg. if you have multiple JRE's/JDK's installed)

eg.

security.provider.2=org.bouncycastle.jce.provider.BouncyCastleProvider

(and renumber the security providers below it - don't put it as the highest priority provider).

  • or you can add BouncyCastle programmatically, as you were trying to do above, but in this case the security policy $JAVA_HOME/jre/lib/security/java.policy should be "unlimited" (you can probably download an unlimited policy file from the Java homepage).
婴鹅 2024-12-04 13:52:35

就我而言,它曾经工作得很好,但后来我在尝试使用 BC 时遇到了 ClassNotFoundException。我重新启动Tomcat,然后一切正常。

我认为如果您重新部署应用程序(就像开发时经常做的那样),它就会停止工作。 JNI 是另一个遭受这个问题困扰的东西。

在我们的例子中这不是问题。我们从不重新部署测试和生产系统。我更喜欢将 jar 与应用程序一起发送,而不是手动将其复制到容器 lib 目录。

In my case it worked fine one time, but then later I got ClassNotFoundException when trying to use BC. I restarted Tomcat and then it worked fine.

I think if you redeploy the app, like you do often while developing, it stops working. JNI is another thing which suffers from this problem.

In our case this isn't a problem. We never redeploy on the test and production systems. I prefer shipping the jar with the app instead of having to manually copy it to the container lib directory.

新雨望断虹 2024-12-04 13:52:35

您需要在 lib 文件夹中添加 javapns.jar 和 bcprove-jdk15on-1.47.jar 文件

import javapns.Push;

import org.apache.log4j.BasicConfigurator;

public class SendPushNotification {

  public static void main(String[] args) {

    try {

        BasicConfigurator.configure();
        Push.alert("Hello World!", "Cetificate.p12", "password", false,
                "mydeviceToken");

    } catch (Exception e) {
        System.out.println(e);
    }

   }

}

You need to add javapns.jar and bcprove-jdk15on-1.47.jar file in your lib folder

import javapns.Push;

import org.apache.log4j.BasicConfigurator;

public class SendPushNotification {

  public static void main(String[] args) {

    try {

        BasicConfigurator.configure();
        Push.alert("Hello World!", "Cetificate.p12", "password", false,
                "mydeviceToken");

    } catch (Exception e) {
        System.out.println(e);
    }

   }

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