2015-10-07 21 views
1

我正在開發JavaCard小程序。小程序生成RSA公鑰和在構造和使用APDU命令加密一些字節數組私鑰:JavaCard小程序不能使用RSA加密

public RSATestApplet() { 
    keyPair = new KeyPair(KeyPair.ALG_RSA_CRT, KeyBuilder.LENGTH_RSA_2048); 
    keyPair.genKeyPair(); 
    rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); 
    rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); 

    cipher = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, false); 

    register(); 
} 

而且主要方法是:

private void encryptData(APDU apdu) { 
    if (!rsaPublicKey.isInitialized()) { 
     ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED); 
    } 
    byte[] apduBuffer = apdu.getBuffer(); 
    apdu.setIncomingAndReceive(); 

    cipher.init(rsaPrivateKey, Cipher.MODE_ENCRYPT); 
    byte[] encryptedBuffer = new byte[apduBuffer.length]; 
    Util.arrayFillNonAtomic(encryptedBuffer, (short) 0, 
      (short) encryptedBuffer.length, (byte) 0xAA); 
    cipher.doFinal(encryptedBuffer, (short) 0, (short) encryptedBuffer.length, apduBuffer, (short) 0); 
    // Just for testing send 120 bytes 
    apdu.setOutgoingAndSend((short) 0, (short) 120); 
} 

當我嘗試安裝小程序APDU響應是6E00(其意味着:沒有精確的診斷)。

我認爲cipher.doFinal()執行時可能會出現問題。

我試着與其他小程序,一切工作正常。

我編譯我的小應用程序與JavaCard的2.2.1和Java 1.2

你有任何想法,這是怎麼回事?

回答

4

我堅信在安裝applet期間發生的錯誤與您的encryptData方法無關。

我建議你在你的構造函數中使用try catch來捕獲JCVM引發的異常。例如,當您創建KeyPair對象時,如果平臺不支持算法和密鑰長度,則可能會引發錯誤。

你可以嘗試這樣的事情:

try { 
    keyPair = new KeyPair(KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_2048); 
} catch (CryptoException e) { 
    short reason = e.getReason(); 
    ISOException.throwIt(reason); 
} 
+0

您好!感謝您提供有用的建議。我已經發現原因是我的卡不支持RSA_2048。但在文檔RSA_2048支持DDA而不是SDA。事情是我不知道什麼是DDA和SDA以及如何在此模式下安裝applet。我會問問我 – raiym

+0

http://stackoverflow.com/questions/33015169/differents-between-sda-and-dda-in-javacard – raiym