JavacardOS will not accept order any more, please contact our partner Feitian online Store:
https://ftsafe.en.alibaba.com/index.html

RSA encryption problem

JavaCard Applet Development Related Questions and Answers.
kineri
Posts: 19
Joined: Thu Dec 10, 2015 2:04 am
Points :184
Contact:

RSA encryption problem

Post by kineri » Fri Jun 30, 2017 12:04 am

I have generated a key pair in the applet constructor and also instantiated a RSA cipher.
If I try to encrypt the same plain text two times with the public key, I obtain two different cipher texts.

What 's the problem? Could anyone figure me out? Thanks.

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

Re: RSA encryption problem

Post by wousim » Fri Jun 30, 2017 2:31 am

Show us code pls.

kineri
Posts: 19
Joined: Thu Dec 10, 2015 2:04 am
Points :184
Contact:

Re: RSA encryption problem

Post by kineri » Fri Jun 30, 2017 4:07 am

Sorry. I forgot to attach my code

Code: Select all

package rsaapplet;

import javacard.framework.*;
import javacard.security.*;
import javacardx.crypto.Cipher;

public class RSAApplet extends javacard.framework.Applet {

     // class of instructions
     private final static byte CLA = (byte) 0x80;

     // RSA instructions
     private final static byte EXPORT_RSA_PUBLIC_MOD = (byte) 0xF0;
     private final static byte EXPORT_RSA_PUBLIC_EXP = (byte) 0xF2;

     //only for test
     private final static byte RSA_ENCODE = (byte) 0xD2;
     private final static byte RSA_DECODE = (byte) 0xD4;

     private final byte SIGN_DATA = (byte) 0xC0;

     // RSA
     private KeyPair keyPair;
     private RSAPrivateCrtKey rsa_privateKey;
     private RSAPublicKey rsa_publicKey;

     private RSAPublicKey otherPartyPublicKey;

     private Cipher rsaCipher = null;
     private Cipher rsaDecipher = null;

     private Signature signature = null;

     private final static short ARRAY_SIZE = 128;

     private byte[] outBuffer;

     /**
     * Constructor
     */
     private RSAApplet(byte[] bArray, short bOffset, byte bLength) {
          // create a transient buffer
          this.outBuffer =
               JCSystem.makeTransientByteArray(
                    ARRAY_SIZE,
                    JCSystem.CLEAR_ON_DESELECT);

          // RSA
          keyPair = new KeyPair(KeyPair.ALG_RSA_CRT, (short) 1024);

          // generate RSA key pair
          keyPair.genKeyPair();

          // get private key
          rsa_privateKey = (RSAPrivateCrtKey) keyPair.getPrivate();
          // get public key
          rsa_publicKey = (RSAPublicKey) keyPair.getPublic();

          // Signature object
          signature = Signature.getInstance(Signature.ALG_RSA_MD5_PKCS1, false);
          // initialize the signature object with the appropriate Key for signing
          signature.init(rsa_privateKey, Signature.MODE_SIGN);

          // get a RSA cipher
          rsaCipher = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, false);
         
          // create an uninitialized cryptographic key
          otherPartyPublicKey =
               (RSAPublicKey) KeyBuilder.buildKey(
                    KeyBuilder.TYPE_RSA_PUBLIC,
                    KeyBuilder.LENGTH_RSA_1024,
                    false);
     }

     public static void install(byte[] bArray, short bOffset, byte bLength) {
          (new RSAApplet(bArray, bOffset, bLength)).register();
     } // install

     public boolean select() {
          //pin.reset();
          return true;
     } // select

     public void process(APDU apdu) {
          byte[] buf = apdu.getBuffer();
          // the selectingApplet() is used in the applet process method to distinguish
          // the SELECT APDU command, which selected this applet, from all other SELECT
          // APDU commands. Returns true if this applet is being selected
          if (selectingApplet()) {
               ISOException.throwIt(ISO7816.SW_NO_ERROR);
          }
          // verify if the applet can accept this APDU message
          if (buf[ISO7816.OFFSET_CLA] != CLA) {
               ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
          }
          switch (buf[ISO7816.OFFSET_INS]) {

               case EXPORT_RSA_PUBLIC_MOD :
                    exportPublicModulus(apdu);
                    break;
               case EXPORT_RSA_PUBLIC_EXP :
                    exportPublicExponent(apdu);
                    break;

               case RSA_ENCODE :
                    rsa_encode(apdu);
                    break;
               case RSA_DECODE :
                    rsa_decode(apdu);
                    break;
               case SIGN_DATA :
                    signData(apdu);
                    break;

               default :
                    ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
          }
     }

     private void exportPublicModulus(APDU apdu) {
          byte buffer[] = apdu.getBuffer();

          // get the exponent and store it in the apdu buffer
          short modLen = rsa_publicKey.getModulus(buffer, (short) 0);

          // send data
          apdu.setOutgoingAndSend((short) 0, (short) modLen);
     }

     private void exportPublicExponent(APDU apdu) {
          byte buffer[] = apdu.getBuffer();

          // get the exponent and store it in the apdu buffer
          short expLen = rsa_publicKey.getExponent(buffer, (short) 0);

          // send data
          apdu.setOutgoingAndSend((short) 0, (short) expLen);
     }

     private void rsa_encode(APDU apdu) {
          byte buffer[] = apdu.getBuffer();

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

          // initialize the cipher for encryption
          rsaCipher.init(rsa_publicKey, Cipher.MODE_ENCRYPT);

          short ret =
               rsaCipher.doFinal(
                    buffer,
                    (short) ISO7816.OFFSET_CDATA,
                    byteRead,
                    buffer,
                    (short) 0);

          // send results
          apdu.setOutgoingAndSend((short) 0, ret);

     }

     private void rsa_decode(APDU apdu) {
          byte buffer[] = apdu.getBuffer();

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

          // initialize the cipher for encryption
          rsaCipher.init(rsa_privateKey, Cipher.MODE_DECRYPT);

          short ret =
               rsaCipher.doFinal(
                    buffer,
                    (short) ISO7816.OFFSET_CDATA,
                    byteRead,
                    buffer,
                    (short) 0);

          // send results
          apdu.setOutgoingAndSend((short) 0, ret);

     }

     
     private void signData(APDU apdu) {
          byte buffer[] = apdu.getBuffer();

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

          short outLen =
               signature.sign(
                    buffer,
                    ISO7816.OFFSET_CDATA,
                    byteRead,
                    buffer,
                    (short) 0);

          // send out signed data
          apdu.setOutgoingAndSend((short) 0, (short) outLen);

     }

}


Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 8 guests

JavaCard OS : Disclaimer