生成密钥

原文: https://docs.oracle.com/javase/tutorial/security/toolfilex/step2.html

在签署包含contract文件的Contract.jar JAR 文件之前,如果您还没有合适的密钥,则需要生成密钥。您需要使用私钥对 JAR 文件进行签名,并且您的收件人需要相应的公钥来验证您的签名。

本课假定您还没有密钥对。您将创建一个名为examplestanstore的密钥库,并使用新生成的公钥/私钥对(使用证书中的公钥)创建一个条目。

现在假装你是斯坦史密斯,并且你在 Example2 公司的法律部门工作。在命令窗口中键入以下内容以创建名为examplestanstore的密钥库并为 Stan Smith 生成密钥:

  1. keytool -genkey -alias signLegal -keystore examplestanstore

密钥库工具会提示您输入密钥库密码,可分辨名称信息和密钥密码。以下是提示;粗体表示您应该键入的内容。

  1. Enter keystore password: <password>
  2. What is your first and last name?
  3. [Unknown]: Stan Smith
  4. What is the name of your organizational unit?
  5. [Unknown]: Legal
  6. What is the name of your organization?
  7. [Unknown]: Example2
  8. What is the name of your City or Locality?
  9. [Unknown]: New York
  10. What is the name of your State or Province?
  11. [Unknown]: NY
  12. What is the two-letter country code for this unit?
  13. [Unknown]: US
  14. Is <CN=Stan Smith, OU=Legal, O=Example2, L=New York, ST=NY, C=US> correct?
  15. [no]: y
  16. Enter key password for <signLegal>
  17. (RETURN if same as keystore password):

前面的keytool命令在执行命令的同一目录中创建名为examplestanstore的密钥库(假设指定的密钥库尚不存在)并为其分配输入的密码。该命令为实体生成公钥/私钥对,其专有名称的公用名为 Stan Smith ,组织单位为 Legal

您刚刚创建的自签名证书包括公钥和专有名称信息。 (自签名证书是由与证书中的公钥对应的私钥签名的证书。)此证书有效期为 90 天。如果未指定 -validity 选项,则这是默认有效期。证书与别名signLegal引用的密钥库条目中的私钥相关联。为私钥分配输入的密码。

自签名证书对于开发和测试应用程序很有用。但是,会警告用户使用不受信任的证书对应用程序进行签名,并询问他们是否要运行该应用程序。要使用户更有信心运行应用程序,请使用由认可的证书颁发机构颁发的证书。