Java无法使用security.addProvider(bouncy castle)

Java无法使用security.addProvider(bouncy castle),java,security,encryption,bouncycastle,Java,Security,Encryption,Bouncycastle,我正试图与bouncyCastle建立一个密钥协议。它需要EC,因为我正在使用javacards。 不幸的是,当我使用Security.addProvider(new BouncyCastleProvider())时,它给出了一个错误: 如果我省略该行,它将给出一个错误: Exception in thread "main" java.security.NoSuchProviderException: no such provider: BC at sun.security.jca.Ge

我正试图与bouncyCastle建立一个密钥协议。它需要EC,因为我正在使用javacards。 不幸的是,当我使用
Security.addProvider(new BouncyCastleProvider())时,它给出了一个错误

如果我省略该行,它将给出一个错误:

Exception in thread "main" java.security.NoSuchProviderException: no such provider: BC
    at sun.security.jca.GetInstance.getService(Unknown Source)
    at javax.crypto.JceSecurity.getInstance(JceSecurity.java:97)
    at javax.crypto.KeyAgreement.getInstance(KeyAgreement.java:230)
    at pract_LCP.keyAgreement.<init>(keyAgreement.java:30)
    at pract_LCP.main.main(main.java:12)

您的jre文件夹中是否有无限制的JCE策略?更新了美国政策和地方政策?你是说这个问题?您可以在问题下方看到一些选项,例如共享编辑标志。在那里你可以编辑它。我发现了问题:我必须在我的安全文件中指定security.provider.N=org.bouncycastle.jce.provider.BouncyCastleProvider。谢谢你给我指明了正确的方向!
Exception in thread "main" java.security.NoSuchProviderException: no such provider: BC
    at sun.security.jca.GetInstance.getService(Unknown Source)
    at javax.crypto.JceSecurity.getInstance(JceSecurity.java:97)
    at javax.crypto.KeyAgreement.getInstance(KeyAgreement.java:230)
    at pract_LCP.keyAgreement.<init>(keyAgreement.java:30)
    at pract_LCP.main.main(main.java:12)
package pract_LCP;

import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import org.bouncycastle.*;
import javax.crypto.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class keyAgreement {
    Security.addProvider(new BouncyCastleProvider());
    //Security.addProvider(new BouncyCastleProvider());

    private String StringPrivatekey="23dc5cf316583c2e60e36c405b10d03541f4a76f6da2852d";
    private String StringPublicKey="47595243a6737a00fe5d911c1a3305be5c108bf4f68b01945c40358143f28077fad4a8a51f190b6d8bb8b580c88011335";
    private PrivateKey convertPrivateKey(String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException{
        byte[] privateKeyBytes=privateKey.getBytes();
        KeyFactory kf = KeyFactory.getInstance("EC"); // or "EC" or whatever
        PrivateKey converted = kf.generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes));
        return converted;
    }
    private PublicKey convertPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException{
        byte[] publicKeyBytes=publicKey.getBytes();
        KeyFactory kf = KeyFactory.getInstance("EC"); // or "EC" or whatever
        PublicKey converted = kf.generatePublic(new PKCS8EncodedKeySpec(publicKeyBytes));
        return converted;
    }
    public keyAgreement() throws InvalidKeyException, IllegalStateException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException{
    KeyAgreement aKeyAgree = KeyAgreement.getInstance("ECDH", "BC");
    aKeyAgree.init(convertPrivateKey(StringPrivatekey));
    aKeyAgree.doPhase(convertPublicKey(StringPublicKey), true);
    byte[] aSecret = aKeyAgree.generateSecret();
    String str = new String(aSecret, StandardCharsets.UTF_8);
    System.out.println("keyagr"+str);
    }

}