Page 1 of 1

How to use keyBuilder class

Posted: Mon Jan 23, 2017 4:24 am
by Methen
Hi all. I have to simulate DDA authentication for my project. I placed my private and public keys inside byte[] variables in applet, and in order to encrypt and decrypt messages, I should convert these byte arrays to PublicKey and PrivateKey objects. javacard.security defines a key factory class keyBuilder, but I don't know how to use this class, to reconstruct my keys, which are in byte array. Does anyone know this?

Re: How to use keyBuilder class

Posted: Mon Jan 23, 2017 5:19 am
by UNKNwYSHSA
You can use like following (This is sample of RSAPublicKey)

Code: Select all

// Create one key instance;
RSAPublicKey rsaPuKey = (RSAPublicKey) KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC, keysize, false);
// Set key value with key value bytes: rsa public key N;
rsaPuKey.setModulus(modulusBuffer, modulusOffset, modulusLength);
// Set key value with key value bytes: rsa public key E;
rsaPuKey.setExponent(exponentBuffer, exponentOffset, exponentLength);
// You can use the key now ...

Re: How to use keyBuilder class

Posted: Tue Jan 24, 2017 4:01 am
by mabel
It's recommended to keep keys in Key objects than in arrays, because keys can be protected by the underlying platform, but arrays cannot.

Re: How to use keyBuilder class

Posted: Tue Jan 24, 2017 4:07 am
by choimillen
Build a key using keybuilder methods and cast:

RSAPublicKey k1 = (RSAPublicKey)KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC, KeyBuilder.LENGTH_RSA_1024);

Then, use:
k1.setExponent(bytearray, offsettoexponent);
k1.setModulus(bytearray, offsettomodulus);

k1.getExponent(destination, wheretostore);
...

Same for others.