I can't decrypt a text encrypted(useing RSA) with publickey on mobile

hi all, I got some problem with my code,

when I wanna use the JSR177 of J2ME to do something about Decryption

here is some of my code as following:

KeyFactory kf = KeyFactory.getInstance("RSA");

byte[] publickeyEncode

x509EncodedKeySpec keyspec =new X509EncodedKeySpec(publickeyEncode)

PublicKey pubkey = kf.generatePublic(Keyspec)

as using that, we can renew our publickey which is from the server.

but there are Exception when I use the publickey to do decryption.

java.security.InvalidKeyException

at com.sun.satsa.crypto.RSACipher.init(+31)

at javax.crypto.Cipher.init(+30)

at javax.crypto.Cipher.init(+7)

at KEYback.startApp(+210)

at javax.microedition.midlet.MIDletProxy.startApp(+7)

at com.sun.midp.midlet.Scheduler.schedule(+270)

at com.sun.midp.main.Main.runLocalClass(+28)

at com.sun.midp.main.Main.main(+116)

however, the problem is solved when I change the DECRYPTO_MODE into ENCRYPTO_MODE

so, is it impossible to do decryption with publickey on the mobile?

[1202 byte] By [JK_taiwana] at [2007-10-3 11:33:09]
# 1
> so, is it impossible to do decryption with> publickey on the mobile?From the mathematical point of view you can of course use a public key for decryption but in the RSA cipher you always encrypt with the public key and decrypt with the private key. Jan
JPJavaa at 2007-7-15 14:00:03 > top of Java-index,Java Mobility Forums,Consumer and Commerce...
# 2

thank you Jan,

because in the JSR177 apis,there is not "PrivateKey" this class

so we only can use the "PublicKey" to do Decryption on the Mobile and do Encryption on Server with the PrivateKey..

as the result of our test , when the cipher_mode is "Encrypto", the code is run well, but when we turn is to "Decrytpo", the Exception is appear.

JK_taiwana at 2007-7-15 14:00:03 > top of Java-index,Java Mobility Forums,Consumer and Commerce...
# 3

> because in the JSR177 apis,there is not "PrivateKey" this class so we only can use the "PublicKey" to do Decryption on the Mobile and do Encryption on Server with the PrivateKey..

Sorry, I forgot that you are working in the J2ME context.

> as the result of our test , when the cipher_mode is "Encrypto", the code is run well, but when we turn is to "Decrytpo", the Exception is appear.

Did you check that the encryption does return a valid result? Because while reading the JSR177 javadoc I got the feeling that the PublicKey only exists for verifying signatures.

May be you should consider switching to the J2ME Bouncycastle implementation completly (or only for decryption). AFAIK it works idependent of JSR177.

Jan

JPJavaa at 2007-7-15 14:00:03 > top of Java-index,Java Mobility Forums,Consumer and Commerce...