Page 1 of 1
How to send public key to host application
Posted: Thu Dec 24, 2015 5:47 am
by JCaberham
I am using RSA algorithm to create public/private key in my application. Now I want to send the public key to host application. Could any one please help me about how to implement this? Thanks for any answers.
Code: Select all
keys = new KeyPair(KeyPair.ALG_RSA_CRT, KeyBuilder.LENGTH_RSA_2048);
keys.genKeyPair();
rsa_publicKey = (RSAPublicKey) keys.getPublic();
Re: How to send public key to host application
Posted: Thu Dec 24, 2015 10:53 pm
by Bob2002
This projectwill helpful
some code like
Code: Select all
/**
* Generate an assymetric RSA key pair according to ISO7816-8,
* Section 5.1. We only support RSA 1024 bit at the moment, and
* return data in simple TLV data objects, tags 81 and 82.
*
* Successful MSE command has to be performed prior to this one.
*/
private void processGenerateAssymetricKeyPair(APDU apdu) {
// This is only valid in state initial (at the moment)
if(state != STATE_INITIAL) {
ISOException.throwIt(SW_INS_NOT_SUPPORTED);
}
byte[] buf = apdu.getBuffer();
byte p1 = buf[OFFSET_P1];
byte p2 = buf[OFFSET_P2];
if(p1 != (byte)0x80 || p2 != (byte)0x00) {
ISOException.throwIt(SW_INCORRECT_P1P2);
}
if(currentPrivateKey[0] == null) {
ISOException.throwIt(SW_CONDITIONS_NOT_SATISFIED);
}
KeyPair pair = new KeyPair(tempKeyPublic, (RSAPrivateCrtKey)currentPrivateKey[0]);
pair.genKeyPair();
// Sanity check, the KeyPair class should regenerate the keys "in place".
if(pair.getPrivate() != currentPrivateKey[0] || pair.getPublic() != tempKeyPublic) {
ISOException.throwIt(SW_DATA_INVALID);
}
apdu.setOutgoing();
short len = (short)0;
short offset = 0;
buf[offset++] = (byte)0x81;
len = tempKeyPublic.getModulus(buf, (short)(offset+2));
buf[offset++] = (byte)0x81;
buf[offset++] = (byte)len;
offset += len;
buf[offset++] = (byte)0x82;
len = tempKeyPublic.getExponent(buf, (short)(offset+1));
buf[offset++] = (byte)len;
offset += len;
apdu.setOutgoingLength(offset);
apdu.sendBytes((short)0, offset);
}