JDK (Java Development Kit) Tutorials
Dr. Herong Yang, Version 5.00

JcaSignatureTest.java - Signature Test Program

This section provides a tutorial example on how to write a sample program to generate a private key and a public, generate a digital signature with the private key, and verify the signature with the public key.

The following sample program shows you how to use signature algorithms provided by the default provider, generate signatures and verify the signatures using the java.security.Signature class:

/**
 * JcaSignatureTest.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.io.*;
import java.security.*;
class JcaSignatureTest {
   public static void main(String[] a) {
      if (a.length<4) {
         System.out.println("Usage:");
         System.out.println("java JcaSignatureTest input output"
            +" keyAlgo signAlgo");
         return;
      }
      String input = a[0];
      String output = a[1];
      String keyAlgo = a[2];
      String signAlgo = a[3];
      try {
         KeyPair pair = getKeys(keyAlgo);
         PrivateKey priKey = pair.getPrivate();
         PublicKey pubKey = pair.getPublic();
         byte[] sign = sign(input,output,signAlgo,priKey);
         verify(input,signAlgo,sign,pubKey);
      } catch (Exception e) {
         System.out.println("Exception: "+e);
         return;
      }
   }
   private static KeyPair getKeys(String algorithm) throws Exception {
      KeyPairGenerator kg = KeyPairGenerator.getInstance(algorithm);
      int keySize = 512;
      kg.initialize(keySize);
      KeyPair pair = kg.generateKeyPair();
      return pair;
   }
   private static byte[] sign(String input, String output, 
      String algorithm, PrivateKey priKey) throws Exception {
      Signature sg = Signature.getInstance(algorithm);
      sg.initSign(priKey);
      System.out.println();
      System.out.println("Signature Object Info: ");
      System.out.println("Algorithm = "+sg.getAlgorithm());
      System.out.println("Provider = "+sg.getProvider());
      FileInputStream in = new FileInputStream(input);
      int bufSize = 1024;
      byte[] buffer = new byte[bufSize];
      int n = in.read(buffer,0,bufSize);
      int count = 0;
      while (n!=-1) {
         count += n;
         sg.update(buffer,0,n);
         n = in.read(buffer,0,bufSize);
      }
      in.close();
      FileOutputStream out = new FileOutputStream(output);
      byte[] sign = sg.sign();
      out.write(sign);
      out.close();
      System.out.println();
      System.out.println("Sign Processing Info: ");
      System.out.println("Number of input bytes = "+count);
      System.out.println("Number of output bytes = "+sign.length);
      return sign;
   }
   private static boolean verify(String input, String algorithm, 
         byte[] sign, PublicKey pubKey) throws Exception {
      Signature sg = Signature.getInstance(algorithm);
      sg.initVerify(pubKey);
      System.out.println();
      System.out.println("Signature Object Info: ");
      System.out.println("Algorithm = "+sg.getAlgorithm());
      System.out.println("Provider = "+sg.getProvider());
      FileInputStream in = new FileInputStream(input);
      int bufSize = 1024;
      byte[] buffer = new byte[bufSize];
      int n = in.read(buffer,0,bufSize);
      int count = 0;
      while (n!=-1) {
         count += n;
         sg.update(buffer,0,n);
         n = in.read(buffer,0,bufSize);
      }
      in.close();
      boolean ok = sg.verify(sign);
      System.out.println("Verify Processing Info: ");
      System.out.println("Number of input bytes = "+count);
      System.out.println("Verification result = "+ok);
      return ok;
   }
}

Note that:

  • The first part, getKeys(), is designed to generate a private key and a public key of a given encryption algorithm.
  • The second part, sign(), is designed to generate a digital signature of a given input data with the private key.
  • The third part, verify(), is designed to verify the digital signature with the same input data and the public key.

See the next section for testing result of JcaSignatureTest.java.

Last update: 2006.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.3.1 on Windows

 Downloading and Installing JDK 1.4.1 on Windows

 Downloading and Installing JDK 1.5.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

Digital Signature Algorithm and Sample Program

 What Is Digital Signature?

 The Signing Process and the Verification Process

 java.security.Signature - The Data Signing Class

JcaSignatureTest.java - Signature Test Program

 Signature Test Program Result

 JcaSign.java - Signature Generation Sample Program

 JcaVerify.java - Signature Verification Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2008
JcaSignatureTest.java - Signature Test Program