为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?

发布于 2024-09-19 11:16:51 字数 2105 浏览 3 评论 0原文

jar (bcprov-jdk16-145.jar) 已添加到项目中,Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()) 已添加到类中,并且BouncyCastleProvider.PROVIDER_NAME 确实返回“BC”,但 AesFileIo.writeFile() 仍然抛出 java.security.NoSuchProviderException 没有这样的提供程序:BC。有什么想法吗?

import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class AesFileIo {

    private static final String AES_ALGORITHM = "AES/CTR/NoPadding";
    private static final String PROVIDER = BouncyCastleProvider.PROVIDER_NAME;
    private static final byte[] AES_KEY_128 = { // Hard coded for now
        78, -90, 42, 70, -5, 20, -114, 103,
        -99, -25, 76, 95, -85, 94, 57, 54};
    private static final byte[] IV = { // Hard coded for now
        -85, -67, -5, 88, 28, 49, 49, 85,
        114, 83, -40, 119, -65, 91, 76, 108};
    private static final SecretKeySpec secretKeySpec =
            new SecretKeySpec(AES_KEY_128, "AES");
    private static final IvParameterSpec ivSpec = new IvParameterSpec(IV);

    public void AesFileIo() {
        Security.addProvider(new org.bouncycastle.jce.provider
                .BouncyCastleProvider());
    }

    public void writeFile(String fileName, String theFile) {
        try {
            Cipher cipher = Cipher.getInstance(AES_ALGORITHM, PROVIDER);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
            byte[] encrypted = cipher.doFinal(theFile.getBytes());
            ObjectOutputStream os = new ObjectOutputStream(
                new FileOutputStream(fileName));
            os.write(encrypted);
            os.flush();
            os.close();
        } catch (Exception e) {
            StackTraceElement se = new Exception().getStackTrace()[0];
            System.err.println(se.getFileName() + " " + se.getLineNumber()
                    + " " + e);
        }
    }
}

The jar (bcprov-jdk16-145.jar) has been added to the project, Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()) has been added to the class, and BouncyCastleProvider.PROVIDER_NAME does return "BC" but AesFileIo.writeFile() still throws java.security.NoSuchProviderException No such provider: BC. Any ideas?

import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class AesFileIo {

    private static final String AES_ALGORITHM = "AES/CTR/NoPadding";
    private static final String PROVIDER = BouncyCastleProvider.PROVIDER_NAME;
    private static final byte[] AES_KEY_128 = { // Hard coded for now
        78, -90, 42, 70, -5, 20, -114, 103,
        -99, -25, 76, 95, -85, 94, 57, 54};
    private static final byte[] IV = { // Hard coded for now
        -85, -67, -5, 88, 28, 49, 49, 85,
        114, 83, -40, 119, -65, 91, 76, 108};
    private static final SecretKeySpec secretKeySpec =
            new SecretKeySpec(AES_KEY_128, "AES");
    private static final IvParameterSpec ivSpec = new IvParameterSpec(IV);

    public void AesFileIo() {
        Security.addProvider(new org.bouncycastle.jce.provider
                .BouncyCastleProvider());
    }

    public void writeFile(String fileName, String theFile) {
        try {
            Cipher cipher = Cipher.getInstance(AES_ALGORITHM, PROVIDER);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
            byte[] encrypted = cipher.doFinal(theFile.getBytes());
            ObjectOutputStream os = new ObjectOutputStream(
                new FileOutputStream(fileName));
            os.write(encrypted);
            os.flush();
            os.close();
        } catch (Exception e) {
            StackTraceElement se = new Exception().getStackTrace()[0];
            System.err.println(se.getFileName() + " " + se.getLineNumber()
                    + " " + e);
        }
    }
}

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

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

发布评论

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

评论(5

如若梦似彩虹 2024-09-26 11:16:52

我对 Android sdk 不太熟悉,但似乎 android-sdk 附带的 BouncyCastle 提供程序已添加到安全性中。

在 PC 环境中您只需手动添加即可,

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

如果您有权访问 policy 文件,只需添加一个条目,例如:

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

注意 .5 it等于已添加的提供者的序号。

Im not very familiar with the Android sdk, but it seems that the android-sdk comes with the BouncyCastle provider already added to the security.

What you will have to do in the PC environment is just add it manually,

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

if you have access to the policy file, just add an entry like:

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

Notice the .5 it is equal to a sequential number of the already added providers.

我不咬妳我踢妳 2024-09-26 11:16:52

您可以通过使用以下代码编辑 java.security 来添加安全提供程序并创建静态块:

static {
    Security.addProvider(new BouncyCastleProvider());
}

如果您使用的是 Maven 项目,那么您必须为 BouncyCastleProvider 添加依赖项:位于项目的 pom.xml 文件中。

<dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.47</version>
</dependency>

如果您使用普通的java项目,那么您可以从下面给出的链接添加下载bcprov-jdk15on-147.jar并编辑您的类路径。

http://www.java2s.com/Code/Jar/b/Downloadbcprovextjdk15on147jar.htm

You can add security provider by editing java.security with using following code with creating static block:

static {
    Security.addProvider(new BouncyCastleProvider());
}

If you are using maven project, then you will have to add dependency for BouncyCastleProvider as follows in pom.xml file of your project.

<dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.47</version>
</dependency>

If you are using normal java project, then you can add download bcprov-jdk15on-147.jar from the link given below and edit your classpath.

http://www.java2s.com/Code/Jar/b/Downloadbcprovextjdk15on147jar.htm

鸠书 2024-09-26 11:16:52

您可以通过编辑 java.security 添加安全提供程序
通过添加
security.provider.=org.bouncycastle.jce.provider.BouncyCastleProvider

或在类顶部添加一行,

Security.addProvider(new BouncyCastleProvider());

则可以使用下面的行来指定提供程序,同时指定算法

Cipher cipher = Cipher.getInstance("AES", "SunJCE");

如果您使用 Bouncy Castle 等其他提供程序,

Cipher cipher =  Cipher.getInstance("AES", "BC");

you can add security provider by editing java.security
by adding
security.provider.=org.bouncycastle.jce.provider.BouncyCastleProvider

or add a line in your top of your class

Security.addProvider(new BouncyCastleProvider());

you can use below line to specify provider while specifying algorithms

Cipher cipher = Cipher.getInstance("AES", "SunJCE");

if you are using other provider like Bouncy Castle then

Cipher cipher =  Cipher.getInstance("AES", "BC");
风吹雨成花 2024-09-26 11:16:52

我对此的经验是,当我在每次执行中都有这个时,使用提供程序作为这样的字符串就可以了

Security.addProvider(new BounctCastleProvider());
new JcaPEMKeyConverter().setProvider("BC");

但是当我优化并将以下内容放入构造函数中时:

   if(bounctCastleProvider == null) {
      bounctCastleProvider = new BouncyCastleProvider();
    }

    if(Security.getProvider(bouncyCastleProvider.getName()) == null) {
      Security.addProvider(bouncyCastleProvider);
    }

然后我必须像这样使用提供程序,否则我会得到上面的结果错误:

new JcaPEMKeyConverter().setProvider(bouncyCastleProvider);

我正在使用 bcpkix-jdk15on 版本 1.65

My experience with this was that when I had this in every execution it was fine using the provider as a string like this

Security.addProvider(new BounctCastleProvider());
new JcaPEMKeyConverter().setProvider("BC");

But when I optimized and put the following in the constructor:

   if(bounctCastleProvider == null) {
      bounctCastleProvider = new BouncyCastleProvider();
    }

    if(Security.getProvider(bouncyCastleProvider.getName()) == null) {
      Security.addProvider(bouncyCastleProvider);
    }

Then I had to use provider like this or I would get the above error:

new JcaPEMKeyConverter().setProvider(bouncyCastleProvider);

I am using bcpkix-jdk15on version 1.65

铁憨憨 2024-09-26 11:16:52

对于那些使用 Web 服务器的用户,请确保 bcprov-jdk16-145.jar 已安装在您的服务器库中,因为 weblogic 必须将该 jar 放入:

<weblogic_jdk_home>\jre\lib\ext

For those who are using web servers make sure that the bcprov-jdk16-145.jar has been installed in you servers lib, for weblogic had to put the jar in:

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