A digital signature system is mainly divided into three parts: 1. Generate a key pair 2. Signing data on the data sender 3. The data receiver is verified to verify the data signature below to explain the three aspects: 1. Generating a key to generate a key to generate a class KEYPAIRGENERATOR that can be provided with Java, with the getInstance method to create a KeypairGenerator object, initialize with initialize, and finally use the generateKeyPAIR to generate a key pair, the key is generated, you can write it. Document. Once users can use the network or other means, after the user receives the data file and the signature file, you can use the convention to verify the digital signature of the data file, and the data determines whether the transfer in the network is illegally modified. GenerateKeyPair.java/ import java.io. *; import java.security *;. Public class GenerateKeyPair {public static void main (String [] args) {boolean bHelp = false; // sign for show help FileOutputStream fileOut;. // Output key to file BYTE [] Pubkey; // Stores the encoded public key byte byte [] privkey; // stores encoded private key bytes IF (args.length! = 1) {system.out.println ("USAGE: generateKeypair ; system.out.println (" option: "); system.out.println (" : the key name "); system.exit (0 );} try {system.out.println ("generating a key pair ....."); keypairgenarator keygen = keypairgenerator.getInstance ("DSA"); // Using the DSA algorithm Keygen.initialize (1024, New Securerand) )); KeyPair pair = keyGen.generateKeyPair (); PublicKey pub = pair.getPublic (); privateKey priv = pair.getPrivate (); pubkey = pub.getEncoded (); privateKey = priv.getEncoded (); fileOut = new FileOutputStream ("Publickey _" args [0]); fileout.write (); fileout.close (); fileout = new fileoutputstream ("PrivateKey_" args [0]): Fileout.write (PrivateKey); file.out. CLOSE (); system.out. Println ("OK!");} Catch (Exception E) {}}}} 2. Generating Digital Signatures You can read data files from the private key file and convert it to a privatekey object, you can use the KeyFactory class and the PKCS8EncodeKeyspec class. KeyFactory can create an opaque key object using a given honeymoon specification, or the key information in the honeymoon object can be taken in the appropriate format. Signature is an engine class that provides a digital signature algorithm such as DSA or RSA With MD5.
The cleavage-based signature algorithm accepts any size input and a private moon, and generates a shorter (often fixed size) pre-name expiration. Also, the signature and public key reflect any content related to the private key. Signature objects can be used for data signatures, or you can check if a signature is a real signature of the prefabricated data. The first step in signature or teaching research signature is to create a Signature instance. The way to use a specific type of signature algorithm or a way to call the Signature Gerinstance static method. There are two, depending on the purpose of Signature: INITSIGN (...) Used for signing initverify (.....) // Used to verify if SigaRe is initialized to sign, you can provide the money data to the object to generate a signature in calling SIGN. The following will be explained in an example.
///SignData.java import java.io. *; import java.security *;. Import java.security.spec *;. Public class SignData {public static void main (String [] args) {FileOutputStream fileOut; byte b; IF (args.length! = 3) {system.out.println ("Usage: SIGNDATA "); System.out.println ("option:"); system.out.println ": the file name of the private key."); System.out.println (": The filename"); system.out.println (": the filename containing signture data ");} try {System.out.println (". Generating a digital signature .... "); FileInputStream fileIn = new FileInputStream (args [0]); byte [] encodedprivateKey = new byte [fileIn. available ()]; finleIn.read (encodedprivateKey); fileIn.close (); PKCS8EncodeKeySpec privKeySpec = new PKCS8EncodeKeySpec (encodedprivateKey); KeyFactory keyFactory = KeyFactory.getInstance ( "DSA"); PrivateKey privKey = keyFactory.generatePrivate (privateKeySpec); Signature DSA = Signature.GetInstance ("SHA / DSA"); DS A.initsign (Privkey); FileInputStream Fis = New FileInputStream (ARGS [1]); while (fis.available ()! = 0) {b = (byte) fis.read (); dsa.Update (b);} fis.close (); byte [] sig = dsa.sign (); fileout = new fileoutputstream (args [2]); fileout.write (); fileout.close (); system.tem.println ("ok" );} catch (Exception E) {/}}} /// end of signagedata.java// SIGNDATA.JAVA reads the encoded private key bytes from Wenjin, using the PKCS8EncodeKeyspec object to encoded the four months The section is converted to a privatekey object, then use the Signature signature data file to generate a signature data and write the signature data.