title: Generating Keypair using bouncy castle in j2me
body: <p>I have Java code for generating keypair as follows:</p>
  
  <pre><code>KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
  keyGen.initialize(1024);
  KeyPair key = keyGen.generateKeyPair();
  PrivateKey priv = key.getPrivate();
  PublicKey pub = key.getPublic();
  </code></pre>
  
  <p>Now I want to do same in j2me using BC. I have downloaded latest bouncy castle library from <a href="http://www.bouncycastle.org/latest_releases.html" rel="nofollow">Bouncy castle latest release</a>.</p>
  
  <p>Here i want to do same in j2me. Below is my code for generating public and private key for j2me:</p>
  
  <pre><code> RSAKeyPairGenerator generator = new RSAKeyPairGenerator();
       generator.init(new RSAKeyGenerationParameters(new BigInteger("10001", 16),//publicExponent
              SecureRandom.getInstance("SHA1PRNG"),
              1024,
              80
              ));
      AsymmetricCipherKeyPair keyPair = generator.generateKeyPair();
  
      AsymmetricKeyParameter myprivatekey = (AsymmetricKeyParameter) keyPair.getPrivate();
      AsymmetricKeyParameter mypublickey = (AsymmetricKeyParameter) keyPair.getPublic();
  </code></pre>
  
  <p>Now i want to get byte array of public key and private key but there is no such method to get encoded bytes directly from AsymmetricKeyParameter class. By googling I have found below solution to get bytes from the AsymmetricKeyParameter .</p>
  
  <pre><code>   PrivateKeyInfo   pkInfo = PrivateKeyInfoFactory.createPrivateKeyInfo(myprivatekey);
     byte[] privateKey = pkInfo.getEncoded();
  </code></pre>
  
  <p>This code should be work but unfortunately my netbeans showing java.lang.Error: ClassFormatError: when i run this code.</p>
  
  <p>Please help me how to resolve this error or let me know any other alternative by which i can get bytes from AsymmetricCipherKeyPair as i want to send this key to my server using socket connection. FOr your information i m using Netbeans 6.9.1 and j2ME SDK 3.0.5 to build my j2me application.</p>
  
  <p>Any help would be appriciated. Thanks</p>
  
  <p>Here is my full midlet code.</p>
  
  <pre><code>import java.io.IOException;
  import java.math.BigInteger;
  import java.security.SecureRandom;
  
  import javax.microedition.lcdui.Command;
  import javax.microedition.lcdui.CommandListener;
  import javax.microedition.lcdui.Display;
  import javax.microedition.lcdui.Displayable;
  import javax.microedition.lcdui.Form;
  import javax.microedition.midlet.*;
  import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
  import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
  import org.bouncycastle.crypto.generators.RSAKeyPairGenerator;
  import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
  import org.bouncycastle.crypto.params.RSAKeyGenerationParameters;
  import org.bouncycastle.crypto.util.PrivateKeyInfoFactory;
  
  /**
   * @author nirav.bhandari
   */
  public class RSAClientMidlet extends MIDlet implements CommandListener {
  
      public void startApp() {
  
          Form f = new Form("Test");
          Command cmd = new Command("Exit", Command.EXIT, 1);
          f.addCommand(cmd);
  
          f.setCommandListener(this);
          Display d = Display.getDisplay(this);
          d.setCurrent(f);
  
          RSAKeyPairGenerator generator = new RSAKeyPairGenerator();
          generator.init(new RSAKeyGenerationParameters(new BigInteger("10001", 16),//publicExponent
                  SecureRandom.getInstance("SHA1PRNG"),
                  1024,
                  80));
          AsymmetricCipherKeyPair keyPair = generator.generateKeyPair();
  
          AsymmetricKeyParameter myprivatekey = (AsymmetricKeyParameter) keyPair.getPrivate();
          AsymmetricKeyParameter mypublickey = (AsymmetricKeyParameter) keyPair.getPublic();
          System.out.println("Done");
          PrivateKeyInfo pkInfo;
  
          try {
  
             pkInfo = PrivateKeyInfoFactory.createPrivateKeyInfo(myprivatekey);
             // i get classformat error here. un
             byte[] privateKey = pkInfo.getEncoded();
    SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(mypublickey);
  
          } catch (IOException ex) {
              ex.printStackTrace();
          }
  
      }
  
      public void pauseApp() {
      }
  
      public void destroyApp(boolean unconditional) {
          notifyDestroyed();
      }
  
      public void commandAction(Command c, Displayable d) {
          destroyApp(true);
      }
  
      private void show(String string) {
          System.out.println(string);
      }
  }
  </code></pre>
  
  <p>here is my stack trace of errror</p>
  
  <pre><code>    TRACE: &lt;at java.lang.Error: ClassFormatError:  78&gt;, startApp threw an Exception
  java.lang.Error: ClassFormatError:  78
          at RSAClientMidlet.startApp(), bci=123
          at javax.microedition.midlet.MIDletTunnelImpl.callStartApp(), bci=1
          at com.sun.midp.midlet.MIDletPeer.startApp(), bci=7
          at com.sun.midp.midlet.MIDletStateHandler.startSuite(), bci=273
          at com.sun.midp.main.AbstractMIDletSuiteLoader.startSuite(), bci=52
          at com.sun.midp.main.CldcMIDletSuiteLoader.startSuite(), bci=8
          at com.sun.midp.main.AbstractMIDletSuiteLoader.runMIDletSuite(), bci=161
          at com.sun.midp.main.AppIsolateMIDletSuiteLoader.main(), bci=26
  java.lang.Error: ClassFormatError:  78
          at RSAClientMidlet.startApp(), bci=123
          at javax.microedition.midlet.MIDletTunnelImpl.callStartApp(), bci=1
          at com.sun.midp.midlet.MIDletPeer.startApp(), bci=7
          at com.sun.midp.midlet.MIDletStateHandler.startSuite(), bci=273
          at com.sun.midp.main.AbstractMIDletSuiteLoader.startSuite(), bci=52
          at com.sun.midp.main.CldcMIDletSuiteLoader.startSuite(), bci=8
          at com.sun.midp.main.AbstractMIDletSuiteLoader.runMIDletSuite(), bci=161
          at com.sun.midp.main.AppIsolateMIDletSuiteLoader.main(), bci=26
  javacall_lifecycle_state_changed() lifecycle: event is JAVACALL_LIFECYCLE_MIDLET_SHUTDOWN status is JAVACALL_OK
  </code></pre>