Skip to Main Content

Java Card

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Please help. Encryption & Decryption with RSA

843851Mar 8 2005 — edited Mar 11 2005
Hello,

I am trying to write an applet that provides some cryptographic support. Initially I am trying to encrypt and decrypt using RSA keys.
My encryption works fine but decryption is throwing cryptoExceptions depending on what I send it.

Some of my code follows...
// KeyPair keys
RSAPublicKey pub_key;	
RSAPrivateCrtKey pri_key;
		
//RSA Keypair object
KeyPair kp;	//cipher object

Cipher RSAcipher;

// Allocate RSA keypair here to be used in multiple sessions
kp = new KeyPair(KeyPair.ALG_RSA_CRT, (short)1024);
	
RSAcipher = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, true);
kp.genKeyPair();
this.pri_key = (RSAPrivateCrtKey)kp.getPrivate();
this.pub_key = (RSAPublicKey)kp.getPublic();

//ENCRYPTION WORKING FINE AND RETURNING 128BYTE CIPHERTEXT
this.RSAcipher.init(this.pri_key, Cipher.MODE_ENCRYPT);
length = RSAcipher.doFinal(buffer, ISO7816.OFFSET_CDATA, byteRead, buffer, (short)0);
apdu.setOutgoingAndSend((short)0, length);

//DECRYPTION CAUSING EXCEPTION
RSAcipher.init(this.pri_key, Cipher.MODE_DECRYPT);
length = RSAcipher.doFinal(buffer, ISO7816.OFFSET_CDATA, byteRead, buffer, (short)0);
apdu.setOutgoingAndSend((short)0, length);
I figure it has something to do with padding but I just don't understand what. If I send an 8byte message to be decrypted then I get ILLEGAL_USE error and if I send a 128byte message to be decrypted then I get ILLEGAL_VALUE error.

What am I doing wrong?

Thanks in advance,
Ann
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 8 2005
Added on Mar 8 2005
5 comments
399 views