两个可能的答案。
如果您想按照自己的方式行事,请尝试用这个方法解决NoClassDefFoundError
。 android eclipse updated and now app crashes when it trys to run
您也可以改用JSch。我自己在android上可靠地工作。 RSA Encryption forceclosing before generating public/private keys
编辑:下面是使用JSch生成RSA类型密钥对的一个示例。我认为它是PKCS#1,但我对标准不够熟悉。 relevant javadoc就是我要做的。
/**
* Load or generate a RSA keypair to use as a client for the given JSch.
*/
public boolean registerKeyPair(JSch jSch) {
new File(getRootFolder().getAbsolutePath() + "/.ssh").mkdirs();
File privateKey = new File(getRootFolder().getAbsolutePath() + "/.ssh/id_rsa");
File publicKey = new File(getRootFolder().getAbsolutePath() + "/.ssh/id_rsa.pub");
if (!privateKey.exists() || !publicKey.exists()) {
try {
KeyPair keyPair = KeyPair.genKeyPair(jSch, KeyPair.RSA);
keyPair.writePrivateKey(privateKey.getAbsolutePath());
keyPair.writePublicKey(publicKey.getAbsolutePath(), "Machine Shop");
return true;
} catch (JSchException e) {
Log.e("genKeyPair(RSA)", Log.getStackTraceString(e));
} catch (FileNotFoundException e) {
Log.e("genKeyPair(RSA)", Log.getStackTraceString(e));
} catch (IOException e) {
Log.e("genKeyPair(RSA)", Log.getStackTraceString(e));
}
return false;
}
try {
jSch.addIdentity(privateKey.getAbsolutePath());
return true;
} catch (JSchException e) {
Log.w("jSch.addIdentity", Log.getStackTraceString(e));
return false;
}
}
编辑:假设Eclipse。在构建路径中包含JSch jar file,最好作为本地jar(比如在lib文件夹中)。请务必在“订购和导出”选项卡上进行检查。
现在刷新您的项目。
在Android中使用BouncyCastle是非常棘手的,因为Android已经在内部使用了BouncyCastle。 SpongyCastle(https://www.google.com.au/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CCMQFjAA&url=https%3A%2F%2Fgithub.com%2Frtyley%2Fspongycastle&ei=Yt9vT_C0G6O5iQfJ8e2TBg&usg=AFQjCNGhIK5nkAxRvqDovIe8E8RMtCaa6Q&cad= rja)是由于这个原因而创建的,对我来说工作得非常好(虽然我还没有试过你的情况) – Carsten 2012-03-26 03:17:03