我是台湾的学生。我正在学习如何在Android上编程。但是我有一个问题关于使用海绵城堡库在ECDH中生成密钥对。当我启动应用程序时,android系统显示应用程序已停止。
这是我的代码和我的导入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);
}
}
请帮。
我解决了这个问题。它需要通过一种新的方式添加提供者。这样的。
static {Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);}
和生成密钥对的代码:
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();}
谢谢