2 Replies Latest reply on Oct 8, 2012 1:25 PM by 966781

    RSA Decryption Error - Illegal Use

      Hi there,

      i have a crypthographic problem using a JavaCard application and a Java host application.
      On the card i create a cipher object and a private/public key:
      Cipher cipher = Cipher.getInstance(Cipher.ALG_RSA_PKCS1_OAEP, false);
      KeyPair kP = new KeyPair(KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_1984);
      RSAPublicKey app_public_key = (RSAPublicKey) kP.getPublic();
      RSAPrivateKey app_private_key = (RSAPrivateKey) kP.getPrivate();
      There are two functions on the card to send the modulus and the exponent from the public key to the host application using two APDUs.
      private void sendModulus(APDU apdu)
           byte[] buffer = apdu.getBuffer();
           short modLength = app_public_key.getModulus(buffer, (short)ISO7816.OFFSET_CDATA);
           apdu.sendBytesLong(buffer, (short)ISO7816.OFFSET_CDATA, modLength);
      private void sendExponent(APDU apdu)
           byte[] buffer = apdu.getBuffer();
           short expLength = app_public_key.getExponent(buffer, (short)ISO7816.OFFSET_CDATA);
           apdu.sendBytesLong(buffer, (short)ISO7816.OFFSET_CDATA, expLength);
      On the host i request the modulus and the exponent and build the public key:
      public void getAppMod() throws TerminalException
                        //get modulus
           ResponseApdu response = send(new CommandApdu("0x00 0xAA 0x01 0x00"));
           byte[] modulus = response.getData().toByteArray();
                        //get exponent
           ResponseApdu response = send(new CommandApdu("0x00 0xAA 0x02 0x00"));
           byte[] exponent = response.getData().toByteArray();
                        RSAPublicKeySpec kSpec = new RSAPublicKeySpec(new BigInteger(1, mod), new BigInteger(1, exp));
           KeyFactory kFac = KeyFactory.getInstance("RSA");
           RSAPublicKey app_rsa_publicKey = (RSAPublicKey)kFac.generatePublic(kSpec);
      Now i create a cipher object on the host application, encrypt a message with this public key and send it to the card:
      Security.addProvider(new BouncyCastleProvider());
      Cipher cipher = Cipher.getInstance("RSA", "BC");
      cipher.init(Cipher.ENCRYPT_MODE, app_rsa_publicKey);
      byte[] cipherData = cipher.doFinal(bData); //bData is the message and cipherData the encrypted 248byte message.
      On the card now im trying to decrypt it with the private key.
      byte[] buffer = apdu.getBuffer();
      short bytesRead = apdu.setIncomingAndReceive();
      cipher.init(app_private_key, Cipher.MODE_DECRYPT);
      short messageLength = cipher.doFinal(buffer, (short)ISO7816.OFFSET_CDATA, bytesRead, buffer, (short)ISO7816.OFFSET_CDATA);
      But the "doFinal" method throws an "ILLEGAL_USE" Exception...
      I dont know what to do now....
      Is it possible that the "BouncyCastle" Cipher object on the host side does not fit the cipher object on the card side ? because the key was transfered correctlly :(
      is there any provider i can use whre i dont need a free library like bouncycastle ?

      Thanks for helping...

      Edited by: 963778 on 08.10.2012 01:56