JavacardOS will not accept order any more, please contact our partner Feitian online Store:
https://ftsafe.en.alibaba.com/index.html
https://ftsafe.en.alibaba.com/index.html
Is there any way to convert PrivateKey from byte array?
Is there any way to convert PrivateKey from byte array?
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?
Is there any way to convert PrivateKey from byte array?
- UNKNwYSHSA
- Posts: 630
- Joined: Thu May 21, 2015 4:05 am
- Points :3055
- Contact:
Re: Is there any way to convert PrivateKey from byte array?
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.
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:
Now you need a KeyFactory object to do the conversion. That object must be one that works with DSA keys.
Finally, you can use the KeyFactory object to generate a PublicKey from the key specification.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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();
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);
Code: Select all
KeyFactory keyFactory = KeyFactory.getInstance("DSA", "SUN");
Code: Select all
PublicKey pubKey =
keyFactory.generatePublic(pubKeySpec);
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
sense and simplicity
- UNKNwYSHSA
- Posts: 630
- Joined: Thu May 21, 2015 4:05 am
- Points :3055
- Contact:
Re: Is there any way to convert PrivateKey from byte array?
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();
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
Who is online
Users browsing this forum: No registered users and 53 guests