我有生成密钥对的Java代码,如下所示:
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair key = keyGen.generateKeyPair();
PrivateKey priv = key.getPrivate();
PublicKey pub = key.getPublic();
现在我想在j2me中使用BC做同样的事情。我从Bouncy castle latest release下载了最新的充气城堡图书馆。
我想在j2me中做同样的事情。下面是我为j2me生成公钥和私钥的代码:
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();
现在我想获取公钥和私钥的字节数组,但是没有这样的方法直接从AsymmetricKeyParameter类获取编码字节。通过谷歌搜索我发现下面的解决方案从AsymmetricKeyParameter获取字节。
PrivateKeyInfo pkInfo = PrivateKeyInfoFactory.createPrivateKeyInfo(myprivatekey);
byte[] privateKey = pkInfo.getEncoded();
此代码应该可以正常工作,但不幸的是我的netbeans显示java.lang.Error:ClassFormatError:当我运行此代码时。
请帮助我如何解决此错误或让我知道任何其他替代方法,我可以从AsymmetricCipherKeyPair获取字节,因为我想使用套接字连接将此密钥发送到我的服务器。使用Netbeans 6.9.1和j2ME SDK 3.0.5来构建我的j2me应用程序。
任何帮助都会得到满足。感谢
这是我的完整midlet代码。
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);
}
}
这是我的错误的错误跟踪
TRACE: <at java.lang.Error: ClassFormatError: 78>, 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