本文整理汇总了Java中javacard.security.KeyPair.genKeyPair方法的典型用法代码示例。如果您正苦于以下问题:Java KeyPair.genKeyPair方法的具体用法?Java KeyPair.genKeyPair怎么用?Java KeyPair.genKeyPair使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javacard.security.KeyPair
的用法示例。
在下文中一共展示了KeyPair.genKeyPair方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generatePair
import javacard.security.KeyPair; //导入方法依赖的package包/类
/**
* @param keypair
* @return
*/
public short generatePair(KeyPair keypair) {
try {
sw = AppletUtil.keypairCheck(keypair);
keypair.genKeyPair();
} catch (CardRuntimeException ce) {
sw = ce.getReason();
}
return sw;
}
示例2: GenerateKeyPairRSA
import javacard.security.KeyPair; //导入方法依赖的package包/类
private void GenerateKeyPairRSA(byte[] buffer) {
byte prv_key_nb = buffer[ISO7816.OFFSET_P1];
if ((prv_key_nb < 0) || (prv_key_nb >= MAX_NUM_KEYS))
ISOException.throwIt(SW_INCORRECT_P1);
byte pub_key_nb = buffer[ISO7816.OFFSET_P2];
if ((pub_key_nb < 0) || (pub_key_nb >= MAX_NUM_KEYS))
ISOException.throwIt(SW_INCORRECT_P2);
if (pub_key_nb == prv_key_nb)
ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);
byte alg_id = buffer[OFFSET_GENKEY_ALG];
short key_size = Util.getShort(buffer, OFFSET_GENKEY_SIZE);
byte options = buffer[OFFSET_GENKEY_OPTIONS];
RSAPublicKey pub_key = (RSAPublicKey) getKey(pub_key_nb, KeyBuilder.TYPE_RSA_PUBLIC, key_size);
RSAPrivateKey prv_key = (RSAPrivateKey) getKey(prv_key_nb, alg_id == KeyPair.ALG_RSA ? KeyBuilder.TYPE_RSA_PRIVATE : KeyBuilder.TYPE_RSA_CRT_PRIVATE, key_size);
/* If we're going to overwrite a keyPair's contents, check ACL */
if (pub_key.isInitialized() && !authorizeKeyOp(pub_key_nb,ACL_WRITE))
ISOException.throwIt(SW_UNAUTHORIZED);
if (prv_key.isInitialized() && !authorizeKeyOp(prv_key_nb,ACL_WRITE))
ISOException.throwIt(SW_UNAUTHORIZED);
/* Store private key ACL */
Util.arrayCopy(buffer, OFFSET_GENKEY_PRV_ACL, keyACLs, (short) (prv_key_nb * KEY_ACL_SIZE), KEY_ACL_SIZE);
/* Store public key ACL */
Util.arrayCopy(buffer, OFFSET_GENKEY_PUB_ACL, keyACLs, (short) (pub_key_nb * KEY_ACL_SIZE), KEY_ACL_SIZE);
switch (options) {
case OPT_DEFAULT:
/* As the default was specified, if public key already * exist we
* have to invalidate it, otherwise its parameters * would be used
* in place of the default ones */
if (pub_key.isInitialized())
pub_key.clearKey();
break;
case OPT_RSA_PUB_EXP:
short exp_length = Util.getShort(buffer, OFFSET_GENKEY_RSA_PUB_EXP_LENGTH);
pub_key.setExponent(buffer, OFFSET_GENKEY_RSA_PUB_EXP_VALUE, exp_length);
break;
default:
ISOException.throwIt(SW_INVALID_PARAMETER);
}
/* TODO: Migrate checks on KeyPair on the top, so we avoid resource
* allocation on error conditions */
/* If no keypair was previously used, ok. If different keypairs were
* used, or for 1 key there is a keypair but the other key not, then
* error If the same keypair object was used previously, check keypair
* size & type */
if ((keyPairs[pub_key_nb] == null) && (keyPairs[prv_key_nb] == null)) {
keyPairs[pub_key_nb] = new KeyPair(pub_key, prv_key);
keyPairs[prv_key_nb] = keyPairs[pub_key_nb];
} else if (keyPairs[pub_key_nb] != keyPairs[prv_key_nb])
ISOException.throwIt(SW_OPERATION_NOT_ALLOWED);
KeyPair kp = keyPairs[pub_key_nb];
if ((kp.getPublic() != pub_key) || (kp.getPrivate() != prv_key))
// This should never happen according with this Applet policies
ISOException.throwIt(SW_INTERNAL_ERROR);
// We Rely on genKeyPair() to make all necessary checks about types
kp.genKeyPair();
}
示例3: generateAsymetricKey
import javacard.security.KeyPair; //导入方法依赖的package包/类
/**
* GENERATE KEY APDU implementation.
*/
private void generateAsymetricKey(APDU apdu) {
byte[] buffer = apdu.getBuffer();
//lc=2
if (apdu.setIncomingAndReceive() != 1) {
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
}
KeyPair key = getKey(buffer[ISO7816.OFFSET_CDATA]);
if (buffer[ISO7816.OFFSET_P1] == (byte) 0x81) {
if (!(key.getPublic()).isInitialized()) {
ISOException.throwIt(ISO7816.SW_FILE_NOT_FOUND);
}
} else {
if (!pins[PIN_INDEX_PW3].isValidated()) {
ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
}
JCSystem.beginTransaction();
key.genKeyPair();
if (buffer[ISO7816.OFFSET_CDATA] == (byte)0xB6) {
signatureCounter[0] = 0;
signatureCounter[1] = 0;
signatureCounter[2] = 0;
}
JCSystem.commitTransaction();
}
// Send the TLV data and public exponent using the APDU buffer.
buffer[ISO7816.OFFSET_CDATA] = 0x7F;
buffer[(short) (ISO7816.OFFSET_CDATA + 1)] = 0x49;
buffer[(short) (ISO7816.OFFSET_CDATA + 2)] = (byte) 0x82;
buffer[(short) (ISO7816.OFFSET_CDATA + 5)] = (byte) 0x82;
short length = ((RSAPublicKey) key.getPublic()).getExponent(
buffer, (short) (ISO7816.OFFSET_CDATA + 7));
buffer[(short) (ISO7816.OFFSET_CDATA + 6)] = (byte) length;
short pos = (short) (ISO7816.OFFSET_CDATA + 7 + length);
buffer[pos] = (byte) 0x81;
buffer[(short) (pos + 1)] = (byte) 0x82;
Util.setShort(buffer, (short) (pos + 2), RSA_KEY_LENGTH_BYTES);
Util.setShort(buffer, (short) (ISO7816.OFFSET_CDATA + 3),
(short) (pos + RSA_KEY_LENGTH_BYTES - ISO7816.OFFSET_CDATA - 1));
apdu.setOutgoingAndSend(ISO7816.OFFSET_CDATA, (short) (length + 11));
// And the modulus using get response.
Util.setShort(commandChainingBuffer, TEMP_GET_RESPONSE_LENGTH, RSA_KEY_LENGTH_BYTES);
((RSAPublicKey) key.getPublic()).getModulus(commandChainingBuffer, TEMP_GET_RESPONSE_DATA);
// Skip leading zero byte.
if (commandChainingBuffer[TEMP_GET_RESPONSE_DATA] == 0) {//��==0 ��GetModulusʧ��
Util.setShort(commandChainingBuffer, TEMP_GET_RESPONSE_OFFSET,
(short) (TEMP_GET_RESPONSE_DATA + 1));
} else {
Util.setShort(commandChainingBuffer, TEMP_GET_RESPONSE_OFFSET, TEMP_GET_RESPONSE_DATA);
}
commandChainingBuffer[TEMP_INS] = buffer[ISO7816.OFFSET_INS];
ISOException.throwIt(ISO7816.SW_BYTES_REMAINING_00);
}
示例4: process
import javacard.security.KeyPair; //导入方法依赖的package包/类
public void process(APDU apdu) throws ISOException {
if(selectingApplet()) {
return;
}
short sendlen = 0;
short recvlen = apdu.setIncomingAndReceive();
byte[] buf = apdu.getBuffer();
byte ins = buf[ISO7816.OFFSET_INS];
KeyPair pair = null;
byte operation = 0;
if((ins & 0x0f) == 0x01) {
operation = GENERATE;
} else if((ins & 0x0f) == 0x02) {
operation = SIGN;
} else {
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
switch(ins & 0xf0) {
case 0x00:
pair = brainpoolp256r1;
break;
case 0x10:
pair = secp256r1;
break;
case 0x20:
pair = brainpoolp320r1;
break;
case 0x30:
pair = brainpoolp256t1;
break;
case 0x40:
pair = secp256k1;
break;
case 0x50:
pair = gost2001;
break;
case 0x60:
pair = frp256v1;
break;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
if(operation == GENERATE) {
pair.genKeyPair();
ECPublicKey pubKey = (ECPublicKey) pair.getPublic();
sendlen = pubKey.getW(buf, _0);
} else if(operation == SIGN) {
signature.init(pair.getPrivate(), Signature.MODE_SIGN);
sendlen = signature.sign(buf, ISO7816.OFFSET_CDATA, recvlen, buf, _0);
}
if(sendlen > 0) {
apdu.setOutgoingAndSend(_0, sendlen);
}
}
示例5: generateAsymetricKey
import javacard.security.KeyPair; //导入方法依赖的package包/类
/**
* GENERATE KEY APDU implementation.
*/
private void generateAsymetricKey(APDU apdu) {
byte[] buffer = apdu.getBuffer();
if (apdu.setIncomingAndReceive() != 2) {
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
}
KeyPair key = getKey(buffer[ISO7816.OFFSET_CDATA]);
if (buffer[ISO7816.OFFSET_P1] == (byte) 0x81) {
if (!(key.getPublic()).isInitialized()) {
ISOException.throwIt(ISO7816.SW_FILE_NOT_FOUND);
}
} else {
if (!pins[PIN_INDEX_PW3].isValidated()) {
ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
}
JCSystem.beginTransaction();
key.genKeyPair();
if (buffer[ISO7816.OFFSET_CDATA] == (byte)0xB6) {
signatureCounter[0] = 0;
signatureCounter[1] = 0;
signatureCounter[2] = 0;
}
JCSystem.commitTransaction();
}
// Send the TLV data and public exponent using the APDU buffer.
buffer[ISO7816.OFFSET_CDATA] = 0x7F;
buffer[(short) (ISO7816.OFFSET_CDATA + 1)] = 0x49;
buffer[(short) (ISO7816.OFFSET_CDATA + 2)] = (byte) 0x82;
buffer[(short) (ISO7816.OFFSET_CDATA + 5)] = (byte) 0x82;
short length = ((RSAPublicKey) key.getPublic()).getExponent(
buffer, (short) (ISO7816.OFFSET_CDATA + 7));
buffer[(short) (ISO7816.OFFSET_CDATA + 6)] = (byte) length;
short pos = (short) (ISO7816.OFFSET_CDATA + 7 + length);
buffer[pos] = (byte) 0x81;
buffer[(short) (pos + 1)] = (byte) 0x82;
Util.setShort(buffer, (short) (pos + 2), RSA_KEY_LENGTH_BYTES);
Util.setShort(buffer, (short) (ISO7816.OFFSET_CDATA + 3),
(short) (pos + RSA_KEY_LENGTH_BYTES - ISO7816.OFFSET_CDATA - 1));
apdu.setOutgoingAndSend(ISO7816.OFFSET_CDATA, (short) (length + 11));
// And the modulus using get response.
Util.setShort(commandChainingBuffer, TEMP_GET_RESPONSE_LENGTH, RSA_KEY_LENGTH_BYTES);
((RSAPublicKey) key.getPublic()).getModulus(commandChainingBuffer, TEMP_GET_RESPONSE_DATA);
// Skip leading zero byte.
if (commandChainingBuffer[TEMP_GET_RESPONSE_DATA] == 0) {
Util.setShort(commandChainingBuffer, TEMP_GET_RESPONSE_OFFSET,
(short) (TEMP_GET_RESPONSE_DATA + 1));
} else {
Util.setShort(commandChainingBuffer, TEMP_GET_RESPONSE_OFFSET, TEMP_GET_RESPONSE_DATA);
}
commandChainingBuffer[TEMP_INS] = buffer[ISO7816.OFFSET_INS];
ISOException.throwIt(ISO7816.SW_BYTES_REMAINING_00);
}