使用BouncyCastleProvider进行密钥交换,报JCE cannot authenticate the provider SC

kidbei 发布于 2016/01/28 11:33
阅读 586
收藏 0

代码如下:

public class ECDHTest {
    static {
        Security.addProvider(new org.spongycastle.jce.provider.BouncyCastleProvider());
    }

    public static void main(String[] args) {
        try{
            KeyPairGenerator g = KeyPairGenerator.getInstance("ECDH", "SC");

            org.spongycastle.jce.spec.ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("prime192v1");
            g.initialize(ecSpec, new SecureRandom());

            KeyPair kp1 = g.generateKeyPair();
            KeyPair kp2 = g.generateKeyPair();

            // Alice generated this
            byte[] privK1 = kp1.getPrivate().getEncoded();
            byte[] pubK1= kp1.getPrivate().getEncoded();

            // Bob generated this
            byte[] privK2 = kp2.getPrivate().getEncoded();
            byte[] pubK2= kp2.getPrivate().getEncoded();


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

            PrivateKey privateKey = KeyFactory.getInstance("ECDH", "SC").generatePrivate(new PKCS8EncodedKeySpec(privK1));
            PublicKey publicKey = KeyFactory.getInstance("ECDH", "SC").generatePublic(new X509EncodedKeySpec(pubK2));


            keyAgreement.init(privateKey);
            keyAgreement.doPhase(publicKey, true);


        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }


}
编辑了java.security文件也没有用,我在idea下进行开发

加载中
返回顶部
顶部