为什么java.security.NoSuchProviderException没有这样的提供者:BC? [英] Why java.security.NoSuchProviderException No such provider: BC?
问题描述
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 = {//现在硬编码
78,-90,42,70,-5,20,-114,103,
-99, 25,76,95,-85,94,57,54}
private static final byte [] IV = {//现在硬编码
-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);
}
}
}
我不是很熟悉 android
sdk,但它认为 android-sdk
BouncyCastle
提供商已添加到安全性。
您将在PC环境中做什么只是添加手动,
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());如果您可以访问策略
文件,则
< ,只需添加一个条目:
security.provider.5 = org.bouncycastle.jce.provider.BouncyCastleProvider
请注意 .5
的已添加的提供商。
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);
}
}
}
Im not very familiar with the android
sdk, but it seeems 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
Please notice the .5
it is equal to a sequential number of the already added providers.
这篇关于为什么java.security.NoSuchProviderException没有这样的提供者:BC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!