This discussion is archived
2 Replies Latest reply: Sep 11, 2012 4:53 AM by 948184 RSS

RSA decryption throws CryptoException.ILLEGAL_VALUE

948184 Newbie
Currently Being Moderated
Hi everybody,

here comes parts of my code:
        /* Card's RSA Pair */
     private KeyPair rsa_keys;
     private RSAPublicKey card_rsa_pu_key;
     private RSAPrivateKey card_rsa_pr_key;


        private CardInitialization() {
          rsa_keys = new KeyPair(KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_512);
          rsa_keys.genKeyPair();
          card_rsa_pu_key = (RSAPublicKey) rsa_keys.getPublic();
          card_rsa_pr_key = (RSAPrivateKey) rsa_keys.getPrivate();
          
          cipher = Cipher.getInstance(Cipher.ALG_RSA_PKCS1 , false);
     }

      private void rsaEncrypt(APDU apdu){

          byte[] buf = apdu.getBuffer();
                short length = apdu.setIncomingAndReceive(); //the length of enctypted data is 6 bytes
          apdu.setOutgoing();
          
          cipher.init(card_rsa_pu_key, Cipher.MODE_ENCRYPT);
          short n = cipher.doFinal(buf, (short)0, length, buf, (short)0);
          
          apdu.setOutgoingLength(n);
          apdu.sendBytes((short)0, n);
     }

        private void rsaDecrypt(APDU apdu){
          
          byte[] buf = apdu.getBuffer();
          short length = apdu.setIncomingAndReceive(); // the length is exact 64 bytes
          apdu.setOutgoing();
          
          short n = 0;
     
          cipher.init(card_rsa_pr_key, Cipher.MODE_DECRYPT);
          
          try{
               n = cipher.doFinal(buf, (short)0, length, buf, (short)0);
          }catch(CryptoException ce){
               switch (ce.getReason()){
               case CryptoException.UNINITIALIZED_KEY:
                    ISOException.throwIt((short)0x4561); return;
               case CryptoException.ILLEGAL_USE:
                    ISOException.throwIt((short)0x4562); return;
               case CryptoException.ILLEGAL_VALUE: // fails
                    ISOException.throwIt((short)0x4563); return;
               case CryptoException.INVALID_INIT:
                    ISOException.throwIt((short)0x4564); return;
               case CryptoException.NO_SUCH_ALGORITHM:
                    ISOException.throwIt((short)0x4565); return;
               }
          }
          
          apdu.setOutgoingLength(n);
          apdu.sendBytes((short)0, n);
     }
....and the decryption method throws CryptoException.ILLEGAL_VALUE...

Does any one have an idea why it does not work?

Thanks in advance
BR
/L
  • 1. Re: RSA decryption throws CryptoException.ILLEGAL_VALUE
    Lyolik Newbie
    Currently Being Moderated
    Hi,

    Are you sure that method:
    short length = apdu.setIncomingAndReceive(); //the length of data being encrypted is 6 bytes
    returns a value length=6 ?
    Please show your C-APDU.

    I think you should write in your code like:
    short n = cipher.doFinal(buf, (short)5, length, buf, (short)0);
    in both
    private void rsaEncrypt(APDU apdu)
    and
    private void rsaDecrypt(APDU apdu)
    methods.

    Edited by: Lyolik on 10.09.2012 8:44

    Edited by: Lyolik on 10.09.2012 12:15
  • 2. Re: RSA decryption throws CryptoException.ILLEGAL_VALUE
    948184 Newbie
    Currently Being Moderated
    Hi,
    thank you for your answer!

    I am 100% sure that it 6. I importerade extra library and ran System.out.println(length)... :)

    But the problem is not in the encryption but decryption...
    I wonder if I can initialize the same instance of the cipher twice...

    The method init in class cipher also can throw crypto exception. So I am not really sure...

    BR
    /L

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points