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!

Rsa Decrypt Not Working.. Please Help

MayankDtuNov 29 2013 — edited Dec 4 2013

Hi everyone,

     I am new to java card development. I have been trying to use RSA encryption in my Applet. I finally got the encryption working but the decryption is showing Illegal Use Error. Any kind of help is appreciated.

public class RSAsample extends Applet{

byte[] rsaPublic = new byte[4];

byte[] rsaPrivate = new byte[4];

byte[] rsaPublicMod = new byte[4];

RSAPrivateKey pri ;

RSAPublicKey pub ;

private final static byte INS_ENCRYPT = (byte) 0x20;

private final static byte INS_DECRYPT = (byte) 0x30;

KeyPair pair ;

private RSAsample() {

  super();

  register();

}

public static void install(byte bArray[], short bOffset, byte bLength)

  throws ISOException {

  new RSAsample();

}

public boolean select() {

  pair = new KeyPair(KeyPair.ALG_RSA, (short) 512);

  pair.genKeyPair();

pri = (RSAPrivateKey) pair.getPrivate();

pub = (RSAPublicKey) pair.getPublic();

  return super.select();

}

public void deselect() {

  super.deselect();

}

   public void process(APDU apdu) throws ISOException {

    byte[] buffer = apdu.getBuffer();

    switch(buffer[ISO7816.OFFSET_INS])

    {

    case INS_ENCRYPT:

    encrypt(apdu);

    break;

    case INS_DECRYPT:

    decrypt(apdu);

    break;

    }

   }

private void decrypt(APDU apdu) {

  byte[] buffer = apdu.getBuffer();

  byte byteRead = (byte)(apdu.setIncomingAndReceive());

  short size = 0;

  try{

  Cipher cipher = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, false);

  cipher.init(pri, Cipher.MODE_DECRYPT);

  size =  cipher.doFinal(buffer, ISO7816.OFFSET_CDATA, (short)byteRead, buffer,

  (short) 0);

  }

  catch(CryptoException e)

  {

  switch(e.getReason())

  {

  case CryptoException.ILLEGAL_USE:

  ISOException.throwIt(ISO7816.SW_DATA_INVALID);

  break;

  case CryptoException.ILLEGAL_VALUE:

  ISOException.throwIt(ISO7816.SW_FILE_INVALID);

  break;

  case CryptoException.INVALID_INIT:

  ISOException.throwIt(ISO7816.SW_FILE_NOT_FOUND);

  break;

  case CryptoException.NO_SUCH_ALGORITHM:

  ISOException.throwIt(ISO7816.SW_FILE_INVALID);

  break;

  case CryptoException.UNINITIALIZED_KEY:

  ISOException.throwIt(ISO7816.SW_FILE_FULL);

  break;

  default:

  ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);

  break;

  }

  }

  apdu.setOutgoing();

  apdu.setOutgoingLength(size);

  apdu.sendBytes((short)0, size);

}

private void encrypt(APDU apdu) {

  byte[] buffer = apdu.getBuffer();

  byte byteRead = (byte)(apdu.setIncomingAndReceive());

  short size = 0;

  try{

  Cipher cipher = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, false);

  cipher.init(pub, Cipher.MODE_ENCRYPT);

  size =  cipher.doFinal(buffer, ISO7816.OFFSET_CDATA, (short)byteRead, buffer,

  (short) 0);

  }

  catch(CryptoException e)

  {

  switch(e.getReason())

  {

  case CryptoException.ILLEGAL_USE:

  ISOException.throwIt(ISO7816.SW_DATA_INVALID);

  break;

  case CryptoException.ILLEGAL_VALUE:

  ISOException.throwIt(ISO7816.SW_FILE_INVALID);

  break;

  case CryptoException.INVALID_INIT:

  ISOException.throwIt(ISO7816.SW_FILE_NOT_FOUND);

  break;

  case CryptoException.NO_SUCH_ALGORITHM:

  ISOException.throwIt(ISO7816.SW_FILE_INVALID);

  break;

  case CryptoException.UNINITIALIZED_KEY:

  ISOException.throwIt(ISO7816.SW_FILE_FULL);

  break;

  default:

  ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);

  break;

  }

  }

  apdu.setOutgoing();

  apdu.setOutgoingLength(size);

  apdu.sendBytes((short)0, size);

}

  

  

  

  

  

  

  

  

  

  

}

Also if this is wrong can you share a sample code using RSA Encryption and Decryption.

Thanks

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 1 2014
Added on Nov 29 2013
1 comment
1,620 views