I am developing a JavaCard applet. Applet generates RSA public and private keys in constructor and with APDU command encrypt some byte array:
public RSATestApplet() {
keyPair = new KeyPair(KeyPair.ALG_RSA_CRT, KeyBuilder.LENGTH_RSA_2048);
keyPair.genKeyPair();
rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
cipher = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, false);
register();
}
And main method is:
private void encryptData(APDU apdu) {
if (!rsaPublicKey.isInitialized()) {
ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
}
byte[] apduBuffer = apdu.getBuffer();
apdu.setIncomingAndReceive();
cipher.init(rsaPrivateKey, Cipher.MODE_ENCRYPT);
byte[] encryptedBuffer = new byte[apduBuffer.length];
Util.arrayFillNonAtomic(encryptedBuffer, (short) 0,
(short) encryptedBuffer.length, (byte) 0xAA);
cipher.doFinal(encryptedBuffer, (short) 0, (short) encryptedBuffer.length, apduBuffer, (short) 0);
// Just for testing send 120 bytes
apdu.setOutgoingAndSend((short) 0, (short) 120);
}
And when I try to install applet APDU response is 6E00 (which means: No precise diagnosis).
I think problem may occurs when cipher.doFinal() is executing.
I tried with other applets and everything works fine.
I compile my applet with JavaCard 2.2.1 and Java 1.2
Do you have any idea what's going on?