java.security.NoSuchAlgorithmException:没有这样的算法:提供者BC的ECDH

时间:2017-06-02 19:00:14

标签: java android bouncycastle

我在Android中有这个例外,虽然我已经下载了无限制的JCE策略,但我添加了lib bcprov。我还添加了一行:

Security.addProvider(new BouncyCastleProvider());

请帮助我知道缺少什么。我读了其他帖子,发现他们已经完成了上述工作。该应用程序在loadprivatekey崩溃,在行:

ECParameterSpec params = ECNamedCurveTable.getParameterSpec(CurveName);)

doECDH函数

调用
   public static PrivateKey loadPrivateKey (byte [] data) throws Exception
  {
    Security.addProvider(new BouncyCastleProvider());
     ECParameterSpec params = ECNamedCurveTable.getParameterSpec(CurveName);
    ECPrivateKeySpec prvkey = new ECPrivateKeySpec(new BigInteger(data), 
     params);
    KeyFactory kf = KeyFactory.getInstance("ECDH", "BC");
    return kf.generatePrivate(prvkey);
}

public static String doECDH ( String data_Prv, String data_Pub) throws Exception
{
    byte[] dataPrv = hexToBytes(data_Prv);
    byte[] dataPub = hexToBytes(data_Pub);
    KeyAgreement ka = KeyAgreement.getInstance("ECDH", "BC");
    ka.init(loadPrivateKey(dataPrv));
    ka.doPhase(loadPublicKey(dataPub), true);
    byte [] secret = ka.generateSecret();
    Shared_Key = bytesToHex(secret);
    //SecretKey originalKey = new SecretKeySpec(secret, 0, secret.length, "ECDH");
    return Shared_Key;
}

1 个答案:

答案 0 :(得分:0)

尝试使用spongycastle,将其添加到build.gradle:

compile 'com.madgag.spongycastle:core:1.54.0.0'
compile 'com.madgag.spongycastle:prov:1.54.0.0'
compile 'com.madgag.spongycastle:pkix:1.54.0.0'
compile 'com.madgag.spongycastle:pg:1.54.0.0'

添加此代码:

static {
    Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
}

并使用此:

KeyFactory kf = KeyFactory.getInstance("ECDH", "SC");

和此:

KeyAgreement ka = KeyAgreement.getInstance("ECDH", "SC");