为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3711754/
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
Why java.security.NoSuchProviderException No such provider: BC?
提问by Hymannad
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?
jar (bcprov-jdk16-145.jar) 已添加到项目中,Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider())
已添加到类中,并且BouncyCastleProvider.PROVIDER_NAME
确实返回“BC”但 AesFileIo.writeFile() 仍然抛出java.security.NoSuchProviderException No such provider: 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);
}
}
}
采纳答案by Garis M Suero
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.
我对 Android sdk 不是很熟悉,但似乎提供程序android-sdk
附带的BouncyCastle
已经添加到安全性中。
What you will have to do in the PC environment is just add it manually,
在 PC 环境中你要做的就是手动添加它,
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
if you have access to the policy
file, just add an entry like:
如果您有权访问该policy
文件,只需添加如下条目:
security.provider.5=org.bouncycastle.jce.provider.BouncyCastleProvider
Notice the .5
it is equal to a sequential number of the already added providers.
请注意,.5
它等于已添加的提供程序的序列号。
回答by kapil das
you can add security provider by editing java.security by adding security.provider.=org.bouncycastle.jce.provider.BouncyCastleProvider
您可以通过添加 security.provider.=org.bouncycastle.jce.provider.BouncyCastleProvider 来编辑 java.security 来添加安全提供程序
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
如果您使用的是 Bouncy Castle 等其他提供商,那么
Cipher cipher = Cipher.getInstance("AES", "BC");
回答by mel3kings
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:
对于那些使用 web 服务器的人,请确保 bcprov-jdk16-145.jar 已安装在您的服务器库中,因为 weblogic 必须将 jar 放入:
<weblogic_jdk_home>\jre\lib\ext
回答by Krutik
You can add security provider by editing java.security with using following code with creating static block:
您可以通过使用以下代码编辑 java.security 并创建静态块来添加安全提供程序:
static {
Security.addProvider(new BouncyCastleProvider());
}
If you are using maven project, then you will have to add dependency for BouncyCastleProvideras follows in pom.xml file of your project.
如果您使用的是 maven 项目,那么您必须在项目的 pom.xml 文件中为BouncyCastleProvider添加依赖项,如下所示。
<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.jarfrom the link given below and edit your classpath.
如果您使用的是普通的 java 项目,那么您可以从下面给出的链接中添加下载bcprov-jdk15on-147.jar并编辑您的类路径。
http://www.java2s.com/Code/Jar/b/Downloadbcprovextjdk15on147jar.htm
http://www.java2s.com/Code/Jar/b/Downloadbcprovextjdk15on147jar.htm