【发布时间】:2018-05-27 12:56:21
【问题描述】:
我将一个字节 [] 从主机应用程序发送到 javacard 小程序。但是当我尝试通过命令缓冲区 [ISO7816.OFFSET_CDATA] 将其作为字节 [] 检索时,我被告知我无法将字节转换为字节 []。如何通过命令 APDU 从主机应用程序发送一个字节 [] 并在另一端(javacard 小程序)将其检索为字节 []。看来 buffer[ISO7816.OFFSET_CDATA] 返回字节。请参阅我的 cmets 了解错误发生的位置。
我的想法如下: 主机应用程序将质询作为字节 [] 发送,以由 javacard 小程序签名。请注意,签名要求挑战是字节 []。 javacard标志如下:
private void sign(APDU apdu) {
if(!pin.isValidated()) ISOException.throwIt(SW_PIN_VERIFICATION_REQUIRED);
else{
byte [] buffer = apdu.getBuffer();
byte numBytes = buffer[ISO7816.OFFSET_LC];
byte byteRead =(byte)(apdu.setIncomingAndReceive());
if ( ( numBytes != 20 ) || (byteRead != 20) )
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
byte [] challenge = buffer[ISO7816.OFFSET_CDATA];// error point cannot convert from byte to byte []
byte [] output = new byte [64];
short length = 64;
short x =0;
Signature signature =Signature.getInstance(Signature.ALG_RSA_SHA_PKCS1, false);
signature.init(privKey, Signature.MODE_SIGN);
short sigLength = signature.sign(challenge, offset,length, output, x); // challenge must be a byte []
//This sequence of three methods sends the data contained in
//'serial' with offset '0' and length 'serial.length'
//to the host application.
apdu.setOutgoing();
apdu.setOutgoingLength((short)output.length);
apdu.sendBytesLong(output,(short)0,(short)output.length);
}
}
挑战由主机应用程序发送,如下所示:
byte [] card_signature=null;
SecureRandom random = SecureRandom . getInstance( "SHA1PRNG" ) ;
byte [] bytes = new byte [ 20 ] ;
random . nextBytes ( bytes) ;
CommandAPDU challenge;
ResponseAPDU resp3;
challenge = new CommandAPDU(IDENTITY_CARD_CLA,SIGN_CHALLENGE, 0x00, 0x20,bytes);
resp3= c.transmit(challenge);
if(resp3.getSW()==0x9000) {
card_signature = resp3.getData();
String s= DatatypeConverter.printHexBinary(card_signature);
System.out.println("signature: " + s);
} else System.out.println("Challenge signature error " + resp3.getSW());
【问题讨论】:
标签: digital-signature javacard apdu