Our Online Store have the new products: RFID antenna board. Currently it can work with JC10M24R and JCOP4 card chips.
Compared with normal cards, the antenna board module has a smaller size and fixed holes, which is easy to integrate in the IOT(Internet Of Things) project.

Is there any way to convert PrivateKey from byte array?

JavaCard Applet Development Related Questions and Answers.
sakkiotto
Posts: 12
Joined: Thu Dec 03, 2015 5:29 am
Points :86
Contact:

Is there any way to convert PrivateKey from byte array?

Post by sakkiotto » Mon Dec 07, 2015 4:38 am

I got the byte array using getEncoded() method, but now I have to convert this byte array back to PrivateKey.
Is there any way to convert PrivateKey from byte array?

User avatar
UNKNwYSHSA
Posts: 630
Joined: Thu May 21, 2015 4:05 am
Points :3053
Contact:

Re: Is there any way to convert PrivateKey from byte array?

Post by UNKNwYSHSA » Mon Dec 07, 2015 10:45 pm

Fllowing is from https://docs.oracle.com/javase/tutorial ... step2.html

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Input and Convert the Encoded Public Key Bytes

Next, VerSig needs to import the encoded public key bytes from the file specified as the first command line argument and to convert them to a PublicKey. A PublicKey is needed because that is what the Signature initVerify method requires in order to initialize the Signature object for verification.

First, read in the encoded public key bytes.

Code: Select all

FileInputStream keyfis = new FileInputStream(args[0]);
byte[] encKey = new byte[keyfis.available()]; 
keyfis.read(encKey);

keyfis.close();
Now the byte array encKey contains the encoded public key bytes.

You can use a KeyFactory class in order to instantiate a DSA public key from its encoding. The KeyFactory class provides conversions between opaque keys (of type Key) and key specifications, which are transparent representations of the underlying key material. With an opaque key you can obtain the algorithm name, format name, and encoded key bytes, but not the key material, which, for example, may consist of the key itself and the algorithm parameters used to calculate the key. (Note that PublicKey, because it extends Key, is itself a Key.)

So, first you need a key specification. You can obtain one via the following, assuming that the key was encoded according to the X.509 standard, which is the case, for example, if the key was generated with the built-in DSA key-pair generator supplied by the SUN provider:

Code: Select all

X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey);
Now you need a KeyFactory object to do the conversion. That object must be one that works with DSA keys.

Code: Select all

KeyFactory keyFactory = KeyFactory.getInstance("DSA", "SUN");
Finally, you can use the KeyFactory object to generate a PublicKey from the key specification.

Code: Select all

PublicKey pubKey =
    keyFactory.generatePublic(pubKeySpec);

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
sense and simplicity

User avatar
UNKNwYSHSA
Posts: 630
Joined: Thu May 21, 2015 4:05 am
Points :3053
Contact:

Re: Is there any way to convert PrivateKey from byte array?

Post by UNKNwYSHSA » Mon Dec 07, 2015 10:58 pm

If you want to do this in your javacard applet, you must decode the encoded key data with the encode format (implement X509EncodedKeySpec), get the key data, and build the key (implement KeyFactory).

Notice:
1 The encode format is not only X509!
2 The key is not only one type!

So the simplest way is (example the key as RSAPrivateKey):
1 [java] RSAPrivateKey.getPrivateExponent() => BigInteger;
2 [java] BigInteger.toByteArray() => byte[];
3 Send byte[] to your javacard applet;
4 [javacard]RSAPrivateKey.setExponent();
sense and simplicity

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 47 guests

JavaCard OS : Disclaimer