Skip to main content

Suggestion required regarding generation of Public-Private Key with SATSA

5 replies [Last post]
abc_tom
Offline
Joined: 2008-04-15

Hi

I am planning to implement RSA based encryption in J2ME.

I could get information about SATSA which comes bundled with J2ME. I could get see that there are classes which actually allows you to encrypt with a public key.

However, I could not locate the classes which would allow me to generate the public-private key pair (as I could not find classes available within SATSA or MIDP to do this)

Can somebody please provide me a suggestion or any other alternative to generate the public-private key pair (based on RSA)?

Regards
Ahmad

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sfitzjava
Offline
Joined: 2003-06-15

Next time please don't cross post the same question in most every javame forum. Put it in a section that makes since, someone will get to it. In this case "javame general help" might have been the right section.

Anyway, after a google search "javame satsa rsa public key" the top 3 items seem to address all your needs. Mr. Google is a very smart source to get you started. Once you get through these links you may have more specific implementation questions and we would enjoy hearing them.

http://developers.sun.com/mobility/apis/articles/satsa2/
http://java.sun.com/j2me/docs/satsa-dg/crypto.html
http://java.sun.com/j2me/docs/satsa-dg/AppD.html

Regards,
-Shawn

abc_tom
Offline
Joined: 2008-04-15

Shawn,

My question was regarding the generation of key pair.

Secondly, I had indeed gone through these links - did a quite a lot of exploration on the same in the last two days. Since [u]I could not locate the classes which would help me generate the key pair[/u], I had posted this question.

It would be great if you can give me suggestion on a way to get the key pair generated so that public can be used in a j2me midlet and the private key in a web application (to which data will be submitted by the midlet)

Regards
Ahmad

Message was edited by: abc_tom

sfitzjava
Offline
Joined: 2003-06-15

You generate the keypair on the desktop using the keytool, just like you would for setting up a desktop pub/priv keypair. You take the public key file and include it in your jar file, and then you would read it as a resource from the jarfile into a byte array then use the logic that the link: http://java.sun.com/j2me/docs/satsa-dg/AppD.html
shows in method runCipher().

Or you might want to generate the pub/priv key on the fly on the web server, here again are some google links using the search key of "generate public private RSA keypair java".
http://exampledepot.com/egs/java.security/GenKeyPair.html
http://www.example-code.com/java/rsa_generateKey.asp

Then have a server request over HTTPS (you don't want to send your public key in clear text) send the public key to the client so that the MIDlet does not have any encryption information when deployed for hackers to read from.

-Shawn

abc_tom
Offline
Joined: 2008-04-15

[b]Thank you Shawn..[/b] I will follow your suggestion...

Regards
Ahmad

abc_tom
Offline
Joined: 2008-04-15

Hi Shawn,

I tried following your suggestion. I did the following but was facing a problem at step (g) below:

a) Generated a Key pair with keytool command

b) Extracted the public key and private key seperately and saved it to individual files (referring to the example in http://code.google.com/support/bin/answer.py?answer=71864&topic=12142)

c) Added the public key file to the jar of midlet

d) Read the public key file as a resource into a byte array

e) Used this read byte array to encrypt

f) I could do a system out of the encrypted data onto the console

g) Next, I tried decrypting the data that was encrypted above. I tried doing this in a stand alone java program but faced an error stating InvalidKeySpecification.

I have listed below the code snippet that I am using to decrypt. Here I am trying to read the private key from the file which I had saved in step b above. I encounter this error at the statement-
[b]PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); [/b]
(This is available as part of J2SE which is available in a stand alone program. This method is not avaialble in the java.security.KeyFactory of J2ME. That gives rise to another issue). Kindly, provide some suggestion to my two questions given below

[u]Question - 1[/u]
Can you please let me know what Is wrong with what I am doing. Please let me know your suggestion as we would require to decrypt the encrypted data in a web application (on receiving a https request from the j2me midlet)

The data encryption using a public key from a resource is successful. However, while trying to decrypt, I would require a private key which I had saved to a file as per step b above. But due to some reason the generatePrivate() of KeyFactory is throwing this error.
Please let me know your ideas on this.

[u]Question - 2[/u]

Also, In our case we would require to decrypt the data which is sent from the server application (in encrypted form). For this we would require a private key to decrypt on the midlet. I would be able to read the private key file as a resource in a midlet but the KeyFactory class of java.security package available with J2ME does not have a method generatePrivate().

It would be helpful to me if you can provide some suggestion for both these questions

/***************************************************************************************/

[b]Code Snippet I am using[/b]
====================

try{
String encryptedText = "some encrypted text"
// [b]Note[/b] : I have not assigned the actual encrypted text as it was having some
//characters which were not allowing this message to be posted on this forum

byte[] strDecryptedText = new byte[encryptedText.length()];

String fileName = "d:\\mykeys\\genrsaprivate.key";

FileInputStream keyfis = new FileInputStream(fileName);
byte[] encKey = new byte[keyfis.available()];
keyfis.read(encKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
BASE64Decoder b64 = new BASE64Decoder();
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(b64.decodeBuffer(encKey.toString()));
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
cipher.doFinal(encryptedText.getBytes(), 0, encryptedText.length(), strDecryptedText, 0);
System.out.println(strDecryptedText);
System.out.println("Start");
System.out.println(new String(strDecryptedText));
System.out.println("End");

}catch(Exception objExp){
objExp.printStackTrace();
}

Error occurred:
===============
java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : null
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(Unknown Source)
at java.security.KeyFactory.generatePrivate(Unknown Source)
at client.TestClient.main(TestClient.java:59)
Caused by: java.security.InvalidKeyException: IOException : null
at sun.security.pkcs.PKCS8Key.decode(Unknown Source)
at sun.security.pkcs.PKCS8Key.decode(Unknown Source)
at sun.security.rsa.RSAPrivateCrtKeyImpl.(Unknown Source)
at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(Unknown Source)
at sun.security.rsa.RSAKeyFactory.generatePrivate(Unknown Source)
... 3 more

/***************************************************************************************/

Regards
Ahmad