尝试获取 RSA public 密钥模数时收到 SW 6F 00

Receiving SW 6F 00 when trying to obtain RSA public key modulus

我在使用 Java 卡小程序实现获取 RSA public 密钥模数时遇到问题:用于发送的命令 SEND_PUB(参见案例说明 case SEND_PUB) public 512 位 RSA 密钥 returns 状态字 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);
        }
    }
}  

您收到状态字 6F 00,因为您在 p.getModulus(...); 行访问 p 时收到 NullPointerException。原因是实例字段 p 从未初始化(至少没有使用您在上面的问题中显示的代码),因此是 null.

请注意

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

不将 public 键对象分配给实例字段 p,而是分配给也命名为 p 的局部变量,因此隐藏了实例字段。