2015-04-01 26 views
2

我正在嘗試將一個小程序安裝到J3A040 JCOP卡中。安裝Java卡小程序因RSA密鑰對生成而失敗

由於安裝方法我有以下幾點:

protected MainApplet() { 

    try { 
     // CREATE RSA KEYS AND PAIR  
     m_keyPair = new KeyPair(KeyPair.ALG_RSA_CRT, KeyBuilder.LENGTH_RSA_2048); 
     // STARTS ON-CARD KEY GENERATION PROCESS 
     m_keyPair.genKeyPair(); 
     // OBTAIN KEY REFERENCES 
     m_publicKey = (RSAPublicKey) m_keyPair.getPublic(); 
     m_privateKey = (RSAPrivateKey) m_keyPair.getPrivate(); 
    } catch (CryptoException c) { 
     //this line will give you the reason of problem 
     short reason = c.getReason(); 
     ISOException.throwIt(reason);  // for check 
    } 

    register(); 
} 

安裝總是失敗,出現以下錯誤:

pro.javacard.gp.GPException: Install for Install and make selectable failed SW: 6A80 
     at pro.javacard.gp.GlobalPlatform.check(GlobalPlatform.java:1092) 
     at pro.javacard.gp.GlobalPlatform.installAndMakeSelectable(GlobalPlatform.java:798) 
     at pro.javacard.gp.GPTool.main(GPTool.java:478) 

但是,如果我刪除密鑰對生成,一切工作正常。 我已閱讀卡的規格和它所代表:

. RSA and RSA CRT (1280 up to 2048 bits keys) for en-/decryption and signature generation and verification1 d. RSA CRT key generation (1280 up to 2048 bits keys) in a secured environment

我想這不應該是一個問題。

任何猜測?

回答

3

問題是由於無效的強制轉換造成的:您以中文提示定理格式(ALG_RSA_CRT)要求提供帶私鑰的RSA KeyPair。

這就是爲什麼getPrivate()方法不會返回RsaPrivateKey實例,而是返回RsaPrivateCrtKey實例。投射到RsaPrivateKey會導致6A80狀態字。

所以您應該使用標準的算法:

m_keyPair = new KeyPair(KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_2048); 

,或者使用一個正確的投:

m_publicKey = (RSAPublicKey) m_keyPair.getPublic(); 
m_privateKey = (RSAPrivateCrtKey) m_keyPair.getPrivate(); 
+0

哦,尷尬..你肯定是正確的。謝謝 – 2015-04-02 18:23:00