eclipse junit java.security.NoSuchAlgorithmException:找不到任何支持的提供程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27260435/
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-09-19 22:16:15  来源:igfitidea点击:

junit java.security.NoSuchAlgorithmException: Cannot find any provider supporting

javaeclipseencryption

提问by Toon Leijtens

I'm using JUnit 4 [C:\eclipse\plugins\org.junit_4.11.0.v201303080030\junit.jar] in combination with Eclipse (MARS, Version: Mars Milestone 3 (4.5.0M3) Build id: 20141113-0320.

我正在使用 JUnit 4 [C:\eclipse\plugins\org.junit_4.11.0.v201303080030\junit.jar] 与 Eclipse(MARS,版本:Mars Milestone 3 (4.5.0M3) 构建 ID:20141113-0320。

I have some tests that test a simple class and which work well. But now arrived at the point where I wanted to test my encryption class, which implements the following encrypt function:

我有一些测试可以测试一个简单的类并且效果很好。但现在到了我想测试我的加密类的地步,它实现了以下加密函数:

public String encrypt(String data) {

    try {

        SecretKeySpec KS = new SecretKeySpec(mKeyData, "Blowfish");

        Cipher cipher = Cipher.getInstance("Blowfish/CBC/ZeroBytePadding"); // PKCS5Padding
        cipher.init(Cipher.ENCRYPT_MODE, KS, new IvParameterSpec(mIv));
        return bytesToHex(cipher.doFinal(data.getBytes()));       

    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
    return null;
}

The Crypto class is not sub classed...

Crypto 类不是子类...

public class Crypto {

So to test this Class and more the encrypt function I have designed the following unit test:

所以为了测试这个类和更多的加密功能,我设计了以下单元测试:

package my.junit4.example;

import static org.junit.Assert.*;

import org.junit.Test;

public class CryptoTest {

    @Test
    public void testEncryption() {

        Crypto myCrypto = new Crypto();

        String encodedString = myCrypto.encrypt("Secret");

        assertTrue("The decrypted encrypted word should deliver the original string", encodedString.equals(myCrypto.decrypt(encodedString)));
    }
}

This test is failing with a stack trace:

此测试失败并显示堆栈跟踪:

java.security.NoSuchAlgorithmException: Cannot find any provider supporting Blowfish/CBC/ZeroBytePaddingnull
at javax.crypto.Cipher.getInstance(Cipher.java:535)     at
my.junit.example.encrypt(Crypto.java:35)    at
my.junit.example.CryptoTest.testEncrypt(CryptoTest.java:14)     at

This didn't make much sense to me. But being relatively new to JUnit I suspect the issue is with me not understanding how to formulate these tests. The code works well encryption - decryption in my debugger is giving me the desired outcome. But how can I get this to work with JUnit. What obvious mistake I have made?

这对我来说没有多大意义。但是作为 JUnit 的新手,我怀疑问题在于我不了解如何制定这些测试。该代码在加密方面运行良好 - 在我的调试器中解密给了我想要的结果。但是我怎样才能让它与 JUnit 一起工作。我犯了什么明显的错误?

回答by Maarten Bodewes

You need to add the Bouncy Castle provider to the Java runtime. You can see how to install the provider by looking at the Bouncy Castle wiki page. Neither the Blowfish algorithm nor zero padding is supported out of the box by Java installations.

您需要将 Bouncy Castle 提供程序添加到 Java 运行时。您可以通过查看Bouncy Castle wiki 页面了解如何安装提供程序。Java 安装不支持 Blowfish 算法和零填充。

The following runs fine on my box:

以下在我的盒子上运行良好:

Security.addProvider(new BouncyCastleProvider());

byte[] mKeyData = new byte[16];
byte[] mIv = new byte[8];

SecretKeySpec KS = new SecretKeySpec(mKeyData, "Blowfish");

Cipher cipher = Cipher.getInstance("Blowfish/CBC/ZeroBytePadding");
cipher.init(Cipher.ENCRYPT_MODE, KS, new IvParameterSpec(mIv));

Make sure that the provider is also available to the test framework when it is run. You'll need to put the bcprov-jdk15on-[version].jarin the class path of the runtime before you can install the provider.

确保提供程序在运行时也可用于测试框架。bcprov-jdk15on-[version].jar在安装提供程序之前,您需要将 放在运行时的类路径中。

回答by Rick

The problem is with this line:

问题出在这一行:

Cipher cipher = Cipher.getInstance("Blowfish/CBC/ZeroBytePadding");

The algorithm you're requesting is not supported on your system. Any particular reason you want that specific one?

您的系统不支持您请求的算法。你想要那个特定的原因有什么特别的吗?

The docsspecify the following default implementations:

文档指定了以下默认实现:

  • AES/CBC/NoPadding (128)
  • AES/CBC/PKCS5Padding (128)
  • AES/ECB/NoPadding (128)
  • AES/ECB/PKCS5Padding (128)
  • DES/CBC/NoPadding (56)
  • DES/CBC/PKCS5Padding (56)
  • DES/ECB/NoPadding (56)
  • DES/ECB/PKCS5Padding (56)
  • DESede/CBC/NoPadding (168)
  • DESede/CBC/PKCS5Padding (168)
  • DESede/ECB/NoPadding (168)
  • DESede/ECB/PKCS5Padding (168)
  • RSA/ECB/PKCS1Padding (1024, 2048)
  • RSA/ECB/OAEPWithSHA-1AndMGF1Padding (1024, 2048)
  • RSA/ECB/OAEPWithSHA-256AndMGF1Padding (1024, 2048)
  • AES/CBC/NoPadding (128)
  • AES/CBC/PKCS5Padding (128)
  • AES/ECB/NoPadding (128)
  • AES/ECB/PKCS5Padding (128)
  • DES/CBC/NoPadding (56)
  • DES/CBC/PKCS5Padding (56)
  • DES/ECB/NoPadding (56)
  • DES/ECB/PKCS5Padding (56)
  • DESede/CBC/NoPadding (168)
  • DESede/CBC/PKCS5Padding (168)
  • DESede/ECB/NoPadding (168)
  • DESede/ECB/PKCS5Padding (168)
  • RSA/ECB/PKCS1Padding (1024, 2048)
  • RSA/ECB/OAEPWithSHA-1AndMGF1Padding (1024, 2048)
  • RSA/ECB/OAEPWithSHA-256AndMGF1Padding (1024, 2048)