The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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
. VerSig
需要从指定为第一个命令行参数的文件中导入编码的公钥字节,并将其转换为PublicKey
。A 需要公钥,因为PublicKey
is needed because that is what the Signature
initVerify
method requires in order to initialize the Signature
object for verification.Signature
initVerify
方法需要公钥来初始化Signature
对象进行验证。
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 array 现在字节数组encKey
contains the encoded public key bytes.encKey
包含编码的公钥字节。
You can use a 您可以使用KeyFactory
class in order to instantiate a DSA public key from its encoding. KeyFactory
类从编码中实例化DSA公钥。The KeyFactory
class provides conversions between opaque keys (of type Key
) and key specifications, which are transparent representations of the underlying key material. KeyFactory
类提供不透明键(Key
类型)和键规范之间的转换,键规范是底层键材质的透明表示。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
.)PublicKey
扩展了Key
,因此它本身就是一个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:所以,首先你需要一个关键规范。假设密钥是根据X.509标准编码的,例如,如果密钥是使用SUN提供商提供的内置DSA密钥对生成器生成的,则可以通过以下方式获得密钥:
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.KeyFactory
对象来进行转换。该对象必须与DSA密钥配合使用。
KeyFactory keyFactory = KeyFactory.getInstance("DSA", "SUN");
Finally, you can use the 最后,您可以使用KeyFactory
object to generate a PublicKey
from the key specification.KeyFactory
对象从密钥规范生成PublicKey
。
PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);