简体   繁体   English

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

[英]bouncycastle provider can't find classes needed for algorithm

I'm trying to use bouncycastle to encrypt a file using a public key. 我正在尝试使用bouncycastle使用公钥加密文件。 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. 当我使用PGPEncryptedDataGenerator加密文件时,我得到一个ClassNotFound异常。

It seems the provider can't find this class at runtime, though I know for sure I have its jar... 似乎提供者在运行时找不到这个类,虽然我知道我有它的jar ...

I'm running my app on tomcat. 我在tomcat上运行我的应用程序。 Using maven to handle dependencies - the bouncy castle jars I put are bcpg, bcprov, bcmail, bctsp. 使用maven来处理依赖关系 - 我放的充气城堡罐子是bcpg,bcprov,bcmail,bctsp。 I tried using both the 1.4 and the 1.6 versions without success. 我尝试使用1.4版本和1.6版本都没有成功。 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. 我在maven插件中使用了“依赖层次结构”,用于在pom中进行eclipse和排除,以确保在我的项目中没有多个版本的bouncycastle。

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)

You have a BouncyCastle Security provider installation problem, you need to either 您有一个BouncyCastle安全提供程序安装问题,您需要

  • 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) 将BouncyCastle作为提供者添加到JRE / JDK $JAVA_HOME/jre/lib/security/java.security文件中(确保将其添加到运行时使用的JRE中,例如,如果安装了多个JRE / JDK)

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

In my case it worked fine one time, but then later I got ClassNotFoundException when trying to use BC. 在我的情况下,它工作了一次,但后来我尝试使用BC时得到了ClassNotFoundException。 I restarted Tomcat and then it worked fine. 我重新启动了Tomcat然后它工作正常。

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. JNI是另一个遭受这个问题的事情。

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. 我更喜欢使用应用程序运送jar,而不必手动将其复制到容器lib目录。

You need to add javapns.jar and bcprove-jdk15on-1.47.jar file in your lib folder 您需要在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);
    }

   }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM