|
|
Generating and Verifying Signatures [API] |
NextVerSigneeds to import the encoded public key bytes from the file specified as the first command line argument, and convert them to aPublicKey. APublicKeyis needed because that is what theSignatureinitVerifymethod requires in order to initialize theSignatureobject for verification.First, read in the encoded public key bytes:
FileInputStream keyfis = new FileInputStream(args[0]); byte[] encKey = new byte[keyfis.available()]; keyfis.read(encKey); keyfis.close();Now the byte arrayencKeycontains the encoded public key bytes.You can use a
KeyFactoryin order to instantiate a DSA public key from its encoding. TheKeyFactoryclass provides conversions between opaque keys (of typeKey) and key specifications, which are transparent representations of the underlying key material. (Note: aPublicKeyis aKey, since it extendsKey.)So, first you need a key specification. You can obtain one via the following, assuming 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):
X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey);Now you need a
KeyFactoryobject to do the conversion. It needs to be one that works with DSA keys:KeyFactory keyFactory = KeyFactory.getInstance("DSA", "SUN");Finally, you can use the
KeyFactoryto generate aPublicKeyfrom the key specification:PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
|
|
Generating and Verifying Signatures [API] |