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 encryption algorithm

JavaCard Applet Development Related Questions and Answers.
Bruninoit
Posts: 6
Joined: Tue Jun 14, 2016 2:21 am
Points :66
Contact:

RSA encryption algorithm

Post by Bruninoit » Mon Jul 11, 2016 1:54 am

I want to do RSA encryption & decryption in my applet. And my java card supports T=0 protocol. So can I send the data in an APDU command and also to get the encrypted data in the same APDU's response? Must I write two different commands to send the data for encryption and get the encrypted data?

wousim
Posts: 14
Joined: Tue Feb 16, 2016 10:12 pm
Points :109
Contact:

Re: RSA encryption algorithm

Post by wousim » Tue Jul 12, 2016 1:55 am

For T=0 protocol, you can not receive the data in the same APDU response. You should send GET RESPONSE command to get the data.
It's the limitation of T=0 protocol.

Bruninoit
Posts: 6
Joined: Tue Jun 14, 2016 2:21 am
Points :66
Contact:

Re: RSA encryption algorithm

Post by Bruninoit » Tue Jul 12, 2016 4:50 am

Thanks for your reply.

I also have another question. Here is section of my code. But it can't be able to work. It always threw exception. Could you point me where I made mistake?

Code: Select all

//In constructor
cipherRSA = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, false);

//In Process Method
short len = apdu.setIncomingAndReceive(); // This is the amount of data read from the OS.

while (len < lc)
        {
            len += apdu.receiveBytes(len);
        }
              case ENCRYPT_HASH_INS:
                    encryptHash(apdu, len);
                    break;
                   
               case GET_ENCRYPT_HASH_INS:
                    getEncryptedHash(apdu);
                    break;

/*********************************************************************/
public void encryptHash( APDU apdu, short length)
     {
          byte [] buffer = apdu.getBuffer();
          cipherRSA.init(rsa_PrivateCrtKey, Cipher.MODE_ENCRYPT);
          short cipherLength = cipherRSA.doFinal(buffer, buffer[ISO7816.OFFSET_CDATA], length, buffer, buffer[ISO7816.OFFSET_CDATA]);
          encyrtedHash = JCSystem.makeTransientByteArray(cipherLength, JCSystem.CLEAR_ON_RESET);
          Util.arrayCopyNonAtomic(buffer, buffer[ISO7816.OFFSET_CDATA], encyrtedHash, (short) 0, length);
     }
/*********************************************************************/     
 public void getEncryptedHash(APDU apdu)
     {
          apdu.setOutgoing();
          apdu.setOutgoingLength((short)encyrtedHash.length );
          apdu.sendBytesLong(encyrtedHash, (short)0, (short)(encyrtedHash.length));
         
     }

Tarantino
Posts: 101
Joined: Wed Aug 19, 2015 1:56 am
Points :478
Contact:

Re: RSA encryption algorithm

Post by Tarantino » Wed Jul 13, 2016 1:50 am

There are some problems in your code.

Try the below code. This code will encrypt your data and send the response in one APDU.

Code: Select all

//In constructor
cipherRSA = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, false);

//In Process Method
short len = apdu.setIncomingAndReceive(); // This is the amount of data read from the OS.

while (len < lc)
     {
          len += apdu.receiveBytes(len);
     }
           case ENCRYPT_HASH_INS:
                    encryptHash(apdu, len);
                    break;
 
 /*********************************************************************/
public void encryptHash( APDU apdu, short length)
     {
          byte [] buffer = apdu.getBuffer();
          cipherRSA.init(rsa_PrivateCrtKey, Cipher.MODE_ENCRYPT);
          short cipherLength = cipherRSA.doFinal(buffer, ISO7816.OFFSET_CDATA, length, buffer, ISO7816.OFFSET_CDATA);
         
          apdu.setOutgoing();
          apdu.setOutgoingAndSend(ISO7816.OFFSET_CDATA, cipherLen)
         
     }
The greatest glory in living lies not in never falling, but in rising every time we fall.--Nelson Mandela

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 38 guests

JavaCard OS : Disclaimer