4

I'm trying to generate an RSA public key in the form of PKCS#1 in Android.

There is almost an exact duplicate of this question here: Generating RSA keys in PKCS#1 format in Java

The author never responded with an answer though. I went through the answers, but I haven't been able to find something that works. I've come to the conclusion (unless someone else has a different answer) that it must be done using Bouncy Castle. The only problem I'm having with bouncy castle is actually using it in Android. I "seem" to have imported the jar correctly (I say "seem" because I've never done it before, but it was a fairly painless process and I get no errors in Eclipse) but, then I get an error in log cat.

03-25 22:25:58.780: E/AndroidRuntime(9171): java.lang.NoClassDefFoundError: org.bouncycastle.jce.provider.BouncyCastleProvider
Community
  • 1
  • 1
EGHDK
  • 17,818
  • 45
  • 129
  • 204
  • 2
    It is tricky to use BouncyCastle in Android as Android already uses a mangled BouncyCastle internally. 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) was created for this reasons and works quite well for me (though I haven't tried your case) – Carsten Mar 26 '12 at 03:17

1 Answers1

5

Two possible answers.

If you want to go the way you're going, try this to resolve the NoClassDefFoundError. android eclipse updated and now app crashes when it trys to run

You can also use JSch instead. I have this working reliably on android myself. RSA Encryption forceclosing before generating public/private keys

Edit: Here's an example of using JSch to generate RSA-type keypairs. I think it's PKCS#1, but I'm not familiar enough with the standard. The relevant javadoc is what I'm going off of.

/**
 * 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;           
    }

}

Edit: Assuming Eclipse. Include the JSch jar file in your build path, preferably as a local jar (say in a lib folder). Make sure to check it on the "Order and Export" tab.

Now refresh your project.

Community
  • 1
  • 1
sastraxi
  • 1,330
  • 11
  • 21
  • I think I am going to pursue the BouncyCastle error but, http://www.jcraft.com/jsch/ doesn't seem to mention anything about PKCS#1, or am I just overlooking it? – EGHDK Mar 26 '12 at 02:47
  • I believe it does, but that's just from looking at this: http://epaul.github.com/jsch-documentation/javadoc/com/jcraft/jsch/KeyPairGenRSA.html. In either case, I've updated my answer with an example. Let me know if it's not PKCS#1, and I'll update it! – sastraxi Mar 26 '12 at 02:53
  • Sorry, to be totally honest, I don't know how to go about "importing" Jsch into my application. Little help? – EGHDK Mar 26 '12 at 03:01
  • The `getRootFolder()` methods are causing errors. Everything else is fine. Do I have to save it as a file? – EGHDK Mar 26 '12 at 04:12
  • The `getRootFolder()` method is from another member of my class--I just ripped that method out of my project :) And no, if you look at the javadoc link I posted up there you can navigate around and see in what other ways you can grab the keys. – sastraxi Mar 26 '12 at 04:18
  • Do I need to add anything to the manifest? I still can't get it to work. – EGHDK Mar 26 '12 at 04:57
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9298/discussion-between-sastraxi-and-eghdk) – sastraxi Mar 26 '12 at 05:01