本文介绍了使用海绵城堡库生成ECDH密钥对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在台湾的学生。我正在学习如何编程机器人。
但我有一个关于使用海绵城堡库来产生ECDH密钥对的问题。
当我启动应用程序,Android系统显示应用程序已经停止。

I am a student in Taiwan. I am learning how to programming in Android.but I have a problem about using Spongy Castle library to generate a key pair in ECDH.when I start the app, android system shows the app has stopped.

下面是我的code和我的进口

Here is my code and my import

public class MainActivity<ECParams> extends Activity {
    String msg,Test;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button generator= (Button) findViewById(R.id.key_pair_generator);
        generator.setOnClickListener(ECkeyPairGenerator);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    private Button.OnClickListener ECkeyPairGenerator = new Button.OnClickListener()
    {
        public void onClick(View v) {
            KeyPairGenerator kpg=null;
            ECGenParameterSpec ecParamSpec = new ECGenParameterSpec("secp224k1");
            try {
                kpg = KeyPairGeneratorSpi.getInstance("ECDH", "SC");
            } catch (NoSuchAlgorithmException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchProviderException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                kpg.initialize(ecParamSpec);
            } catch (InvalidAlgorithmParameterException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            KeyPair kpair=kpg.generateKeyPair();
            msg="pp";
            ShowMsg();
        }

    };
    public static String byteArrayToHexString(byte b[]) {
        StringBuffer s = new StringBuffer();
        int LEN = b.length;
        if (b.length != LEN)
            throw new RuntimeException("byteArrayToHexString() " +
                                       "wrong argument length (!="+LEN);
        for (int j = 0; j < b.length; j++) {
            s.append(Integer.toHexString((int)((b[j]>>4)&0x0f)));
            s.append(Integer.toHexString((int)(b[j]&0x0f)));
        }        
        return new String(s);
    }
    public void ShowMsg(){
        Intent intent = new Intent();
        intent.setClass(MainActivity.this, Show.class);
        Bundle bundle = new Bundle();
        bundle.putString("Show", msg);
        intent.putExtras(bundle); 
        startActivity(intent);
    }

}

请帮忙。

推荐答案

我解决这个问题。
它需要通过一种新的方式来补充供应商。像这样的。

I solve that problem.It needs to add provider by a new way. Like this.

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

和code如何生成密钥对:

and the code what to generate a key pair:

    try {
            ECGenParameterSpec ecParamSpec = new ECGenParameterSpec("secp224k1");
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECDH","SC");
            kpg.initialize(ecParamSpec);

            KeyPair kpair=kpg.generateKeyPair();
            pkey=kpair.getPublic();
            skey=kpair.getPrivate();
        }catch(Exception e){e.printStackTrace();}

感谢

这篇关于使用海绵城堡库生成ECDH密钥对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 02:36