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