Help with RSA Encryption using SATSA

Hello,

I am a new to writing code on J2ME . I am trying to encrypt data using

RSA public key on J2ME using SATSA.

I generated the public key using openssl in the PEM format and stored the

key (mypublickey) as a Base64 decoded byte array in my code.

Next, I did the following:

X509EncodedKeySpec test - new X509EncodedKeySpec(mypublickey);

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

PublicKey key = kf.generatePublic(test);

I used this key to encrypt as follows:

cipher c = Cipher.getInstance("RSA");

c.init(Cipher.ENCRYPT_MODE, key);

c.doFinal(data,0,data.length,ciphertext,0);

where byte[] data = "1234567890".getBytes();

I get no errors during this process.

Now, when I try to decrypt the string, I get a padding error as follows:

javax.crypto.BadPaddingException: Data must start with zero

The decode is done on a server.

I tried getting an instance of the cipher with RSA/ECB/NoPadding and this time the decrypt gives junk.

Question 2: The SATSA example online at http://java.sun.com/j2me/docs/satsa-dg/AppD.html

has a public key embedded as a byte array. They haven't explained how

this key is generated. Does someone know?

Question 3: Suppose, I can get the modulus and exponent of the public key is there any way I can convert it to X509EncodedKeySpec so that I can

use the APIs in SATSA?

Thanks in advance for your help. I have been trying to solve this for a lot of time and any help will be greatly appreciated.

[1581 byte] By [gk4321a] at [2007-11-27 2:14:05]
# 1

Just wanted to add my code:

public class test2 {

public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, ShortBufferException {

// TODO Auto-generated method stub

byte [] data = "012345678901234567890123456789ab".getBytes();

Base64 base64 = new Base64();

/*public key generated by

byte [] mypublickey = base64.decode("publickey in PEM format");

byte [] ciphertext = new byte[128];

X509EncodedKeySpec test = new X509EncodedKeySpec(mypublickey);

byte [] myprivatekey = base64.decode("privatekey in pkcs8format");

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

PublicKey pubkey = rsakeyfac.generatePublic(test);

Cipher c1 = Cipher.getInstance("RSA");

c1.init(Cipher.ENCRYPT_MODE, pubkey);

c1.doFinal(data, 0,data.length, ciphertext);

PKCS8EncodedKeySpec pks2 = new PKCS8EncodedKeySpec(myprivatekey);

RSAPrivateCrtKey privkey = (RSAPrivateCrtKey)rsakeyfac.generatePrivate(pks2);

Cipher c2 = Cipher.getInstance("RSA");

c2.init(Cipher.DECRYPT_MODE, privkey);

byte [] decrypteddata = c2.doFinal(ciphertext);

System.out.println("Decrypted String is:"+new String(decrypteddata).trim());

}

}

Error that I get is:

Exception in thread "main" javax.crypto.BadPaddingException: Data must start with zero

at sun.security.rsa.RSAPadding.unpadV15(Unknown Source)

at sun.security.rsa.RSAPadding.unpad(Unknown Source)

at com.sun.crypto.provider.RSACipher.a(DashoA13*..)

at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)

at javax.crypto.Cipher.doFinal(DashoA13*..)

gk4321a at 2007-7-12 2:09:56 > top of Java-index,Security,Cryptography...
# 2

Can someone help me with importing RSA public key say in PEM format into

a Java? The key was created by openssl. I can embed the key into the code. How do I use it as a X509EncodedKey? I am specific about this format as I need to use the code in J2ME (SATSA). I am testing it out on J2SE and I get the bad padding error.

gk4321a at 2007-7-12 2:09:56 > top of Java-index,Security,Cryptography...