为什么java.security.NoSuchProviderException没有这样的提供者: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 = { // 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);
        }
    }
}
我不是很熟悉android的sdk,但它看到android-sdk自带的BouncyCastle提供程序已经添加到安全。

你将在PC环境中做什么只是手动添加它,

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

如果您有权访问策略文件,只需添加如下条目:

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

请注意.5它等于已添加的提供程序的序列号。

http://stackoverflow.com/questions/3711754/why-java-security-nosuchproviderexception-no-such-provider-bc

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:为什么java.security.NoSuchProviderException没有这样的提供者:BC?