Mega Code Archive

 
Categories / Java / Security
 

This program demonstrates how to sign a message with a private DSA key and verify it with the matching public key

/*    This program is a part of the companion code for Core Java 8th ed.    (http://horstmann.com/corejava)    This program is free software: you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the Free Software Foundation, either version 3 of the License, or    (at your option) any later version.    This program is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details.    You should have received a copy of the GNU General Public License    along with this program.  If not, see <http://www.gnu.org/licenses/>. */ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; import java.security.Signature; /**  * This program demonstrates how to sign a message with a private DSA key and verify it with the  * matching public key. Usage:<br>  * java SignatureTest -genkeypair public private<br>  * java SignatureTest -sign message signed private<br>  * java SignatureTest -verify signed public<br>  * @version 1.11 2007-10-06  * @author Cay Horstmann  */ public class SignatureTest {    public static void main(String[] args)    {       try       {          if (args[0].equals("-genkeypair"))          {             KeyPairGenerator pairgen = KeyPairGenerator.getInstance("DSA");             SecureRandom random = new SecureRandom();             pairgen.initialize(KEYSIZE, random);             KeyPair keyPair = pairgen.generateKeyPair();             ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1]));             out.writeObject(keyPair.getPublic());             out.close();             out = new ObjectOutputStream(new FileOutputStream(args[2]));             out.writeObject(keyPair.getPrivate());             out.close();          }          else if (args[0].equals("-sign"))          {             ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));             PrivateKey privkey = (PrivateKey) keyIn.readObject();             keyIn.close();             Signature signalg = Signature.getInstance("DSA");             signalg.initSign(privkey);             File infile = new File(args[1]);             InputStream in = new FileInputStream(infile);             int length = (int) infile.length();             byte[] message = new byte[length];             in.read(message, 0, length);             in.close();             signalg.update(message);             byte[] signature = signalg.sign();             DataOutputStream out = new DataOutputStream(new FileOutputStream(args[2]));             int signlength = signature.length;             out.writeInt(signlength);             out.write(signature, 0, signlength);             out.write(message, 0, length);             out.close();          }          else if (args[0].equals("-verify"))          {             ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[2]));             PublicKey pubkey = (PublicKey) keyIn.readObject();             keyIn.close();             Signature verifyalg = Signature.getInstance("DSA");             verifyalg.initVerify(pubkey);             File infile = new File(args[1]);             DataInputStream in = new DataInputStream(new FileInputStream(infile));             int signlength = in.readInt();             byte[] signature = new byte[signlength];             in.read(signature, 0, signlength);             int length = (int) infile.length() - signlength - 4;             byte[] message = new byte[length];             in.read(message, 0, length);             in.close();             verifyalg.update(message);             if (!verifyalg.verify(signature)) System.out.print("not ");             System.out.println("verified");          }       }       catch (Exception e)       {          e.printStackTrace();       }    }    private static final int KEYSIZE = 512; }