Our Online Store have the new products: RFID antenna board. Currently it can work with JC10M24R and JCOP4 card chips.
Compared with normal cards, the antenna board module has a smaller size and fixed holes, which is easy to integrate in the IOT(Internet Of Things) project.

RSA in java card

Algorithm School

Moderator: UNKNwYSHSA

KevinAli
Posts: 24
Joined: Fri Aug 21, 2015 3:38 am
Points :104
Contact:

RSA in java card

Post by KevinAli » Thu Feb 04, 2016 3:08 am

Does anyone have sample code for RSA encryption / decryption running on java card?

User avatar
mabel
Posts: 237
Joined: Mon May 18, 2015 3:09 am
Points :1705
Contact:

Re: RSA in java card

Post by mabel » Tue Feb 09, 2016 7:43 am

Maybe the code below will be helpful to you.

Code: Select all

  public RSAEncDec(byte[] bArray,short bOffset,byte bLength){
 
  outbuffer = new byte[128];
  inbuffer = new byte[64];
  cipherENC = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, false);
  cipherDEC = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, false);
  rsaPrivateKey = (RSAPrivateCrtKey)KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_CRT_PRIVATE, KeyBuilder.LENGTH_RSA_512, false);
  rsaPublicKey = (RSAPublicKey)KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC, KeyBuilder.LENGTH_RSA_512, false);
  kp = new KeyPair(KeyPair.ALG_RSA_CRT, (short)512);
  kp.genKeyPair();
  rsaPrivateKey = (RSAPrivateCrtKey)kp.getPrivate();
  rsaPublicKey = (RSAPublicKey)kp.getPublic();
  register();
  }
 
  public static void install(byte[] bArray, short bOffset, byte bLength) {

  new RSAEncDec(bArray, (short) (bOffset + 1), bArray[bOffset]);
  }
 
  public void process(APDU apdu) {
  if (selectingApplet()) {
  return;
  }
 
  byte[] buf = apdu.getBuffer();
  switch (buf[ISO7816.OFFSET_INS]) {
          case (byte) 0x00:
                   break;
         case RSAENC:
                  encryptData(apdu);
                  break;
        case RSADEC:
                 decryptData(apdu);
                 break;
       default:
                ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
  }
  }
 
  private void encryptData(APDU apdu){
  short length =0;
  byte[] buffer = apdu.getBuffer();   
  byte incomingLength = (byte) (buffer[ISO7816.OFFSET_LC]);
  if (incomingLength != 8)
       ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
 
    Util.arrayFillNonAtomic(inbuffer, (short)0, (short)64, (byte)0);
    Util.arrayFillNonAtomic(outbuffer,(short)0, (short)128, (byte)0);
 
    Util.arrayCopy(buffer, (short)5, inbuffer, (short)0,incomingLength);
 
    cipherENC.init(rsaPrivateKey, Cipher.MODE_ENCRYPT);
  length = cipherENC.doFinal(inbuffer, (short)0, (short)incomingLength, outbuffer, (short)0);
  Util.arrayCopy(outbuffer, (short)0, buffer, (short)0, length);
  apdu.setOutgoingAndSend( (short)0, length);
  }
 
  private void decryptData(APDU apdu){
  short length = 0;
  byte[] buffer = apdu.getBuffer();
 
  cipherDEC.init(rsaPublicKey, Cipher.MODE_DECRYPT);
  length = cipherDEC.doFinal(outbuffer, (short)0, (short)64, outbuffer, (short)0);
  Util.arrayCopy(outbuffer, (short)0, buffer, (short)0, length);
  apdu.setOutgoingAndSend( (short)0, length);   
  }

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 30 guests

JavaCard OS : Disclaimer