2017-12-01 20 views
-1

자바 카드 애플릿 구현으로 RSA 공개 키 모듈을 얻는 데 문제가 있습니다. 공용 512 비트 RSA 키를 보내는 명령 SEND_PUB (사례문 case SEND_PUB 참조) 상태 단어 6F 00을 반환합니다. 구현에 어떤 문제가 있습니까? 라인 p.getModulus(...);p에 액세스 할 때 당신이 NullPointerException를 얻을 수 있기 때문에RSA 공개 키 모듈을 얻으려고 할 때 SW 6F 00을 받음

public class crypto extends Applet { 


    private static final boolean NO_EXTERNAL_ACCESS = false; 
    private static byte[] file=new byte[128]; 
    private static byte[] SignedFile=new byte[20];  
    private static RSAPublicKey p; 
    private static RSAPublicKey publicKey; 
    private static RSAPrivateKey privateKey; 
    private static KeyPair keyPair; 
    Signature sig; 
    private final static byte ALOC= 0x07; //vérifier le code PIN 
    private final static byte INS_PIN= 0x01; //vérifier le code PIN 
    private final static byte INS_PUK= 0x02; //vérifier le code PUK 
    private final static byte UPD_PIN= 0x03; //modifier le code PIN 
    private final static byte RCV_FILE= 0x04; //recvoir le fichier 
    private final static byte SIGNATURE= 0x05; //Récupérer la clé privée 
    private final static byte SEND_PUB= 0x06; //envoyer la la clé publique 

    public static OwnerPIN pin,puk; 

    public static void install(byte[] bArray, short bOffset, byte bLength) { 

     new crypto(); 
    } 


    protected crypto() { 
     register(); 
     puk = new OwnerPIN(nbre_tentative, length); 
     puk.update(code_puk, (short) 0, length); 

     pin = new OwnerPIN(nbre_tentative, length); 
     pin.update(code_pin, (short) 0, length); 


     // publicKey = (RSAPublicKey) KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC,KeyBuilder.LENGTH_RSA_512,true); 
     // keyPair = new KeyPair(KeyPair.ALG_RSA, (short) publicKey.getSize()); 
     // publicKey = (RSAPublicKey) keyPair.getPublic(); 



    KeyPair rsa_KeyPair = new KeyPair(KeyPair.ALG_RSA_CRT, KeyBuilder.LENGTH_RSA_512); 
     rsa_KeyPair.genKeyPair(); 
     RSAPublicKey p = (RSAPublicKey) rsa_KeyPair.getPublic(); 
     //RSAPrivateKey rsa_PrivateCrtKey 0= (RSAPrivateKey) rsa_KeyPair.getPrivate(); 
     // cipherRSA = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, false); 
     } 


    public void process(APDU apdu) { 
     byte[] buffer = apdu.getBuffer(); 
     if(selectingApplet()) 
      return; 
     if(buffer[ISO7816.OFFSET_CLA] != CLA) 
      ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED); 
     switch (buffer[ISO7816.OFFSET_INS]) 
     { 


      case SEND_PUB : 

      //this is to send the modulus 
       p.getModulus(buffer, ISO7816.OFFSET_CDATA); 
       apdu.setOutgoing(); 
       apdu.setOutgoingLength((short) 64); 
       apdu.sendBytesLong(buffer, ISO7816.OFFSET_CDATA, (short) 64); 

       case SIGNATURE : 
        Signature s = Signature.getInstance(Signature.ALG_RSA_SHA_PKCS1, false); 
        s.init(privateKey, Signature.MODE_SIGN); 
        short sigLen = s.sign(file,(short)0, (short)file.length,SignedFile, (short)0);      
        break;  
      default: 
        ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED); 
     } 
    } 
} 
+0

스위치의 첫 번째 경우에 'break'가 누락 되었습니까? – vojta

답변

2

당신은 상태 단어 6F 00을받을 수 있습니다. 그 이유는 인스턴스 필드 p이 초기화되지 않았기 때문입니다 (적어도 위의 질문에서 보여준 코드가 아님). 따라서 null입니다. 라인

RSAPublicKey p = (RSAPublicKey) rsa_KeyPair.getPublic(); 

가 인스턴스 필드 p뿐만 아니라, 따라서 p 명명하고 로컬 변수에 공개 키 객체를 할당하지 않습니다

주, 인스턴스 필드를 숨 깁니다.

+0

내가 보여준 코드에 문제가있는 것은 아닙니다. 도움을 청하십시오. –

+3

@ AymanR'bati 그런 다음 실제로 사용하는 코드를 보여주세요! 'p'는 여전히 업데이트 된 코드에서'null'입니다. –