Using Spongy Castle library to generate a key pair in ECDH

北慕城南 提交于 2019-12-11 00:28:46

问题


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.

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);
    }

}

Please help.


回答1:


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);}

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();}

thanks



来源:https://stackoverflow.com/questions/16133579/using-spongy-castle-library-to-generate-a-key-pair-in-ecdh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!