Search code examples
javaandroidbouncycastle

Use of spongy castle in android


Hey I am new to java and android. Trying to work out with spongy castle but having some problem. I tried the solution in post this but still getting run time error "Application has stopped unexpectedly. Please Try Again".

Here is the code:

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.ECGenParameterSpec;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

import org.spongycastle.jce.provider.BouncyCastleProvider;

public class MainActivity extends Activity {

static {
    //Security.addProvider(new BouncyCastleProvider());
    Security.insertProviderAt(new BouncyCastleProvider(), 1);
    }

@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) {
            try {
                ECGenParameterSpec ecParamSpec = new ECGenParameterSpec("secp224k1");
                KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECDH","SC");// KeyPairGenerator.getInstance("ECDH","SC");
                kpg.initialize(ecParamSpec);

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

}

when I run debugger then it stops at the statement: "Security.insertProviderAt(new BouncyCastleProvider(), 1);" by giving following error.

Construct a new provider. This should only be required when using runtime registration of the provider using the Security.addProvider() mechanism.

I have added provider in JDK but still no use. Please help me?

Here is the logcat:

11-22 18:07:21.744: E/dalvikvm(572): Could not find class 'org.spongycastle.jce.provider.BouncyCastleProvider', referenced from method com.example.testrsakeygeneration.MainActivity.<clinit>
11-22 18:07:21.804: W/dalvikvm(572): VFY: unable to resolve new-instance 639 (Lorg/spongycastle/jce/provider/BouncyCastleProvider;) in Lcom/example/testrsakeygeneration/MainActivity;
11-22 18:07:21.804: D/dalvikvm(572): VFY: replacing opcode 0x22 at 0x0000
11-22 18:07:21.804: D/dalvikvm(572): VFY: dead code 0x0002-0009 in Lcom/example/testrsakeygeneration/MainActivity;.<clinit> ()V
11-22 18:07:21.834: W/dalvikvm(572): Exception Ljava/lang/NoClassDefFoundError; thrown during Lcom/example/testrsakeygeneration/MainActivity;.<clinit>
11-22 18:07:21.855: W/dalvikvm(572): Class init failed in newInstance call (Lcom/example/testrsakeygeneration/MainActivity;)
11-22 18:07:21.864: D/AndroidRuntime(572): Shutting down VM
11-22 18:07:21.864: W/dalvikvm(572): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-22 18:07:21.934: E/AndroidRuntime(572): FATAL EXCEPTION: main
11-22 18:07:21.934: E/AndroidRuntime(572): java.lang.ExceptionInInitializerError
11-22 18:07:21.934: E/AndroidRuntime(572):  at java.lang.Class.newInstanceImpl(Native Method)
11-22 18:07:21.934: E/AndroidRuntime(572):  at java.lang.Class.newInstance(Class.java:1429)
11-22 18:07:21.934: E/AndroidRuntime(572):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-22 18:07:21.934: E/AndroidRuntime(572):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
11-22 18:07:21.934: E/AndroidRuntime(572):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-22 18:07:21.934: E/AndroidRuntime(572):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-22 18:07:21.934: E/AndroidRuntime(572):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-22 18:07:21.934: E/AndroidRuntime(572):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 18:07:21.934: E/AndroidRuntime(572):  at android.os.Looper.loop(Looper.java:123)
11-22 18:07:21.934: E/AndroidRuntime(572): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-22 18:07:21.934: E/AndroidRuntime(572): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 18:07:21.934: E/AndroidRuntime(572):  at java.lang.reflect.Method.invoke(Method.java:521)
11-22 18:07:21.934: E/AndroidRuntime(572):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-22 18:07:21.934: E/AndroidRuntime(572):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-22 18:07:21.934: E/AndroidRuntime(572):  at dalvik.system.NativeStart.main(Native Method)
11-22 18:07:21.934: E/AndroidRuntime(572): Caused by: java.lang.NoClassDefFoundError: org.spongycastle.jce.provider.BouncyCastleProvider
11-22 18:07:21.934: E/AndroidRuntime(572):  at com.example.testrsakeygeneration.MainActivity.<clinit>(MainActivity.java:22)
11-22 18:07:21.934: E/AndroidRuntime(572):  ... 15 more

Solution

  • In addition to adding the libraries to Eclipse's Build Path, you also need to export them so they are included with your app. Go to the project's properties, choose Java Build Path, select the Order and Export tab, and make sure your libraries are checked.