Documentation

The Java™ Tutorials
Hide TOC
Generate Public and Private Keys生成公钥和私钥
Trail: Security Features in Java SE
Lesson: Generating and Verifying Signatures
Section: Generating a Digital Signature

Generate Public and Private Keys生成公钥和私钥

In order to be able to create a digital signature, you need a private key. (Its corresponding public key will be needed in order to verify the authenticity of the signature.)为了能够创建数字签名,您需要一个私钥。(需要相应的公钥来验证签名的真实性。)

In some cases the key pair (private key and corresponding public key) are already available in files. In that case the program can import and use the private key for signing, as shown in Weaknesses and Alternatives.在某些情况下,密钥对(私钥和相应的公钥)已经在文件中可用。在这种情况下,程序可以导入并使用私钥进行签名,如弱点和替代方案所示。

In other cases the program needs to generate the key pair. A key pair is generated by using the KeyPairGenerator class.在其他情况下,程序需要生成密钥对。使用KeyPairGenerator类生成密钥对。

In this example you will generate a public/private key pair for the Digital Signature Algorithm (DSA). You will generate keys with a 1024-bit length.在这个例子中,您将为数字签名算法(DSA)生成一个公钥/私钥对。您将生成长度为1024位的密钥。

Generating a key pair requires several steps:生成密钥对需要几个步骤:

Create a Key Pair Generator创建密钥对生成器

The first step is to get a key-pair generator object for generating keys for the DSA signature algorithm.第一步是获取一个密钥对生成器对象,用于为DSA签名算法生成密钥。

As with all engine classes, the way to get a KeyPairGenerator object for a particular type of algorithm is to call the getInstance static factory method on the KeyPairGenerator class. 与所有引擎类一样,为特定类型的算法获取KeyPairGenerator对象的方法是调用KeyPairGgenerator类上的getInstance静态工厂方法。This method has two forms, both of which hava a String algorithm first argument; one form also has a String provider second argument.此方法有两种形式,都有一个String algorithm的第一个参数;一个表单还有一个String provider第二个参数。

A caller may thus optionally specify the name of a provider, which will guarantee that the implementation of the algorithm requested is from the named provider. The sample code of this lesson always specifies the default SUN provider built into the JDK.因此,调用者可以选择指定提供者的名称,这将保证所请求的算法的实现来自指定的提供者。本课的示例代码始终指定JDK中内置的默认SUN提供程序。

Put the following statement after the在后面加上以下语句

else try {

line in the file created in the previous step, Prepare Initial Program Structure:在上一步“准备初始程序结构”中创建的文件中的行:

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN");

Initialize the Key Pair Generator初始化密钥对生成器

The next step is to initialize the key pair generator. All key pair generators share the concepts of a keysize and a source of randomness. 下一步是初始化密钥对生成器。所有密钥对生成器都共享密钥大小和随机性来源的概念。The KeyPairGenerator class has an initialize method that takes these two types of arguments.KeyPairGenerator类有一个initialize方法,它接受这两种类型的参数。

The keysize for a DSA key generator is the key length (in bits), which you will set to 1024.DSA密钥生成器的密钥大小是密钥长度(以位为单位),您将其设置为1024。

The source of randomness must be an instance of the SecureRandom class that provides a cryptographically strong random number generator (RNG). 随机性的来源必须是SecureRandom类的一个实例,该类提供了一个加密强的随机数生成器(RNG)。For more information about SecureRandom, see the SecureRandom API Specification and the Java Cryptography Architecture Reference Guide .有关SecureRandom的更多信息,请参阅SecureRandom API规范Java加密体系结构参考指南

The following example requests an instance of SecureRandom that uses the SHA1PRNG algorithm, as provided by the built-in SUN provider. The example then passes this SecureRandom instance to the key-pair generator initialization method.以下示例请求一个使用SHA1PRNG算法的SecureRandom实例,该算法由内置的SUN提供程序提供。然后,该示例将此SecureRandom实例传递给密钥对生成器初始化方法。

SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen.initialize(1024, random);

Some situations require strong random values, such as when creating high-value and long-lived secrets like RSA public and private keys. 某些情况需要强随机值,例如在创建RSA公钥和私钥等高价值和长寿命的秘密时。To help guide applications in selecting a suitable strong SecureRandom implementation, starting from JDK 8 Java distributions include a list of known strong SecureRandom implementations in the securerandom.strongAlgorithms property of the java.security.Security class. 为了帮助指导应用程序选择合适的强SecureRandom实现,从JDK 8开始,Java发行版在java.security.Security类的securerandom.strongAlgorithms属性中包含一个已知的强SecureRandom实现列表。When you are creating such data, you should consider using SecureRandom.getInstanceStrong(), as it obtains an instance of the known strong algorithms.当您创建此类数据时,您应该考虑使用SecureRandom.getInstanceStrong(),因为它可以获得已知强算法的实例。

Generate the Pair of Keys生成密钥对

The final step is to generate the key pair and to store the keys in PrivateKey and PublicKey objects.最后一步是生成密钥对,并将密钥存储在PrivateKeyPublicKey对象中。

KeyPair pair = keyGen.generateKeyPair();
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();

Previous page: Prepare Initial Program Structure
Next page: Sign the Data