Re: Suggestion required regarding generation of Public-Private Key with SAT

[email protected] Fri, 13 Jun 2008 23:42:49 PDT
Newsgroups gmane.comp.java.sun.kvm
Message-ID <21416333.121281213425822417.JavaMail.tomcat@sdcst12.sjc.collab.net>
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.<init>(Unknown Source)
	at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(Unknown Source)
	at sun.security.rsa.RSAKeyFactory.generatePrivate(Unknown Source)
	... 3 more


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

Regards
Ahmad
[Message sent by forum member 'abc_tom' (abc_tom)]

http://forums.java.net/jive/thread.jspa?messageID=280293

===========================================================================
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff KVM-INTEREST".  For general help, send email to
[email protected] and include in the body of the message "help".