Generate digital signature for text file in java

import java.awt.*;
import java.io.*;
import java.security.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
public class frame1 extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JButton Browse,gen;
private JTextField path,comm1;
private Container container;
public static void main (String[] args)
{
frame1 application = new frame1();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setResizable(false);
}
public frame1()
{
super("Generate Signature");

container=getContentPane();
setLayout(null);
path = new JTextField(30);
container.add(path);
path.setBounds(30, 20, 150, 25);
Browse = new JButton("Browse");
container.add(Browse);
Browse.setBounds(190, 20, 80, 25);
gen = new JButton("Generate");
container.add(gen);
gen.setBounds(30, 70, 100, 25);
comm1=new JTextField(50);
container.add(comm1);
comm1.setBounds(30,100,250,25);
comm1.setEditable(false);
String ans5 = "Project by: Akshay Tikekar & Kunal Kumar";
            comm1.setText(ans5);
Browse.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{

      JFileChooser chooser = new JFileChooser();
      chooser.showOpenDialog(null);
      File file = chooser.getSelectedFile();
      final String filenm = file.getPath();
      path.setText(filenm);


gen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
try{

        /* Generate a key pair */

        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");

        keyGen.initialize(1024, random);

        KeyPair pair = keyGen.generateKeyPair();
        PrivateKey priv = pair.getPrivate();
        PublicKey pub = pair.getPublic();


        /* Create a Signature object and initialize it with the private key */

        Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");

        dsa.initSign(priv);

        /* Update and sign the data */

        FileInputStream fis = new FileInputStream(filenm);
        BufferedInputStream bufin = new BufferedInputStream(fis);
        byte[] buffer = new byte[1024];
        int len;
        while (bufin.available() != 0) {
            len = bufin.read(buffer);
            dsa.update(buffer, 0, len);
            };

        bufin.close();

        /* Now that all the data to be signed has been read in,
                generate a signature for it */

        byte[] realSig = dsa.sign();

 
        /* Save the signature in a file */
        FileOutputStream sigfos = new FileOutputStream("signature");
        sigfos.write(realSig);

        sigfos.close();


        /* Save the public key in a file */
        byte[] key = pub.getEncoded();
        FileOutputStream keyfos = new FileOutputStream("publickey");
        keyfos.write(key);

        keyfos.close();

    } catch (Exception e) {
        System.err.println("Caught exception " + e.toString());
    }

}});
}});
setLocation(350,250);
setSize(300,200);
setVisible(true);
}
}

/* NOTE :-Save the code as " frame1.java " and give only text(.txt) file as argument.Signature will be created where frame1.java will be saved.  */

Show your appreciation by liking or sharing the post:
Previous
Next Post »