Exception | java.security.NoSuchProviderException: no such provider: BC

背景

今天在用PGP key做JWT签名和验签的时候,转换报了如下错误:

org.bouncycastle.openpgp.PGPException: exception on setup: java.security.NoSuchProviderException: no such provider: BC

	at org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder$1.get(Unknown Source)
	at org.bouncycastle.openpgp.operator.PGPUtil.makeKeyFromPassPhrase(Unknown Source)
	at org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor.makeKeyFromPassPhrase(Unknown Source)
	at org.bouncycastle.openpgp.PGPSecretKey.extractKeyData(Unknown Source)
	at org.bouncycastle.openpgp.PGPSecretKey.extractPrivateKey(Unknown Source)
	at com.eshare.util.PGPExampleUtil.findSecretKey(PGPExampleUtil.java:55)
	at com.eshare.examples.JwtExampleTest.testJWTSigningAndVerify(JwtExampleTest.java:46)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.security.NoSuchProviderException: no such provider: BC
	at sun.security.jca.GetInstance.getService(GetInstance.java:83)
	at sun.security.jca.GetInstance.getInstance(GetInstance.java:206)
	at java.security.Security.getImpl(Security.java:698)
	at java.security.MessageDigest.getInstance(MessageDigest.java:227)
	at org.bouncycastle.jcajce.util.NamedJcaJceHelper.createDigest(Unknown Source)
	at org.bouncycastle.openpgp.operator.jcajce.OperatorHelper.createDigest(Unknown Source)
	... 29 more

解决方案

经排查,发现目前JDK8不支持BouncyCastleProvider导致,可以通过在静态代码块加入BouncyCastleProvider即可解决

  /**
   * used to fix java.security.NoSuchProviderException: no such provider: BC
   */
  static {
    try {
      Security.addProvider(new BouncyCastleProvider());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

Exception | java.security.NoSuchProviderException: no such provider: BC_第1张图片
同时,如果需要用到BouncyCastle相关类,还需要依赖其对应的依赖包

   
    <dependency>
      <groupId>org.bouncycastlegroupId>
      <artifactId>bcpg-jdk15onartifactId>
      <version>1.62version>
    dependency>
    
    <dependency>
      <groupId>org.bouncycastlegroupId>
      <artifactId>bcprov-jdk15onartifactId>
      <version>1.62version>
    dependency>

你可能感兴趣的:(异常处理)