Creating RSA keys in Android in PKCS # 1 format on Android

I am trying to create an RSA public key in the form of PKCS # 1 in Android.

Here's an almost exact duplicate of this question: Generating RSA keys in PKCS # 1 format in Java

However, the author did not respond to the answer. I went through the answers, but I could not find something that works. I came to the conclusion (if someone else has no other answer) that this should be done using Bouncy Castle. The only problem I encountered with the annoying lock actually uses it on Android. It seems to me that I imported the jar correctly (I say “seems” because I have never done this before, but it was a pretty painless process and I don't get errors in Eclipse), but then I get an error message in log cat .

03-25 22:25:58.780: E/AndroidRuntime(9171): java.lang.NoClassDefFoundError: org.bouncycastle.jce.provider.BouncyCastleProvider 
+4
source share
1 answer

Two possible answers.

If you want to follow the path, try this to resolve NoClassDefFoundError . The eclipse Android app has been updated and now the app crashes when trying to execute

You can use JSch instead. I have reliable work on android. Securing RSA encryption before generating public / private keys

Edit: Here is an example of using JSch to generate RSA keyboard keys. I think this is PKCS # 1, but I'm not familiar enough with the standard. the corresponding javadoc is what I'm leaving.

 /** * 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 jar jar file in your build path, preferably as a local jar (say, in the lib folder). Be sure to check it on the "Order and Export" tab.

Update your project.

+5
source

Source: https://habr.com/ru/post/1403378/


All Articles