JDK Tutorials - Herong's Tutorial Examples - Version 6.02, by Dr. Herong Yang
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) 2014, HerongYang.com, All Rights Reserved. */ 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:
See the next section for testing result of JcaSignatureTest.java.
Last update: 2014.
Table of Contents
Downloading and Installing JDK 1.8.0 on Windows
Downloading and Installing JDK 1.7.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
Encoding Conversion Programs for Encoded Text Files
Datagram Network Communication
DOM (Document Object Model) - API for XML Files
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
The Signing Process and the Verification Process
java.security.Signature - The Data Signing Class
►JcaSignatureTest.java - Signature Test Program
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