Issue about generating 2048 bytes keys with RSA
Posted: Sun Aug 20, 2017 11:50 pm
I need to generates a pair of 2048 bytes keys with the RSA alg in my applet. The APDU response is expected to be the 256 bytes of the public modulus and SW1-SW2 9000. But It returned 0x6F00. I don't know where I made mistake. Could anyone give me some clues and help me out? My code is shown below.
Code: Select all
import javacard.framework.*;
import javacard.security.*;
import javacardx.crypto.*;
public class RSA extends Applet {
public static final byte INS_GEN_KEYS = 0x46;
private static Key privateKey;
public static Key publicKey;
private static KeyPair keyPair;
private RSA() {
}
public static void install(byte bArray[], short bOffset, byte bLength)
throws ISOException {
(new RSA()).register();
}
public void process(APDU apdu) throws ISOException {
byte[] buffer = apdu.getBuffer();
if (selectingApplet()) return ;
switch(buffer[ISO7816.OFFSET_INS])
{
case INS_GEN_KEYS:
genKeys(apdu);
return;
}
}
private void genKeys(APDU apdu) throws CryptoException {
byte[] apduBuffer = apdu.getBuffer();
privateKey = KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PRIVATE,KeyBuilder.LENGTH_RSA_2048, false);
publicKey = KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC,KeyBuilder.LENGTH_RSA_2048, false);
keyPair = new KeyPair(KeyPair.ALG_RSA, (short)publicKey.getSize());
keyPair.genKeyPair();
publicKey = keyPair.getPublic();
apduBuffer[0] = (byte)((RSAPublicKey)publicKey).getModulus(apduBuffer, (short)1);
apdu.setOutgoing(); // set transmission to outgoing data
apdu.setOutgoingLength((short) apduBuffer.length);
apdu.sendBytesLong(apduBuffer, (short) 0, (short) apduBuffer.length);
}
}