Documentation

The Java™ Tutorials
Hide TOC
Generating a Digital Signature生成数字签名
Trail: Security Features in Java SE
Lesson: Generating and Verifying Signatures

Generating a Digital Signature生成数字签名

The GenSig program you are about to create will use the JDK Security API to generate keys and a digital signature for data using the private key and to export the public key and the signature to files. The application gets the data file name from the command line.您即将创建的GenSig程序将使用JDKSecurity API为使用私钥的数据生成密钥和数字签名,并将公钥和签名导出到文件中。应用程序从命令行获取数据文件名。

The following steps create the GenSig sample program.以下步骤创建GenSig示例程序。

  1. Prepare Initial Program Structure准备初始程序结构

    Create a text file named GenSig.java. Type in the initial program structure (import statements, class name, main method, and so on).创建一个名为GenSigjava的文本文件。键入初始程序结构(导入语句、类名、main方法等)。

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

    Generate a key pair (public key and private key). The private key is needed for signing the data. The public key will be used by the VerSig program for verifying the signature.生成密钥对(公钥和私钥)。签名数据需要私钥。公钥将由VerSig程序用于验证签名。

  3. Sign the Data

    Get a Signature object and initialize it for signing. Supply it with the data to be signed, and generate the signature.获取Signature对象并初始化它以进行签名。向其提供要签名的数据,并生成签名。

  4. Save the Signature and the Public Key in Files将签名和公钥保存在文件中

    Save the signature bytes in one file and the public key bytes in another.将签名字节保存在一个文件中,将公钥字节保存在另一个文件。

  5. Compile and Run the Program编译并运行程序


Previous page: Generating and Verifying Signatures
Next page: Prepare Initial Program Structure