Blogger Tips and TricksLatest Tips And TricksBlogger Tricks

Implement the SIGNATURE SCHEME - Digital Signature Standard in java

PROGRAM CODE:


TestDigitalSignature.java


import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;

//import PublicKeyUtil.*;
//import SignatureUtil.*;

public class TestDigitalSignature {
 public static void main(String args[]) throws Exception {
  String file = "emp.txt";

  /* Public key stored in this file */
  String publicKeyFile = "publicKey.txt";

  /* Signature of given file stored here */
  String signatureFile = "siganture.txt";

  /* Signature algorithm to get Signature instance */
  String sigAlgorithm = "SHA1withDSA";

  /* generate public and private keys */
  KeyPair keyPair = PublicKeyUtil.getKeyPair("DSA");
  PublicKey publicKey = keyPair.getPublic();
  PrivateKey privateKey = keyPair.getPrivate();

  /* Generate signature for given file */
  byte signature[] = SignatureUtil.getSignature(file, privateKey,
    sigAlgorithm);

  /* Save public key */
  PublicKeyUtil.saveKey(publicKey, publicKeyFile);

  /* Save signature */
  SignatureUtil.saveSignature(signatureFile, signature);

  // Verify Signature

  /* Read public key from file */
  byte[] pubKeyBytes = PublicKeyUtil.readKeyFromFile(publicKeyFile);

  /* Convert publick key bytes into PublicKey object */
  PublicKey pubKey = PublicKeyUtil.convertArrayToPubKey(pubKeyBytes,
    "DSA");

  /* Read signature from file */
  byte[] receivedSignature = SignatureUtil
    .readSignatureFromFile(signatureFile);

  /* Verify signature */
  Signature verifySignature = Signature.getInstance(sigAlgorithm);

  /* initialize signature object */
  verifySignature.initVerify(pubKey);

  /* Feed data */
  SignatureUtil.feedData(verifySignature, file);

  /* Verify signature */
  boolean isAuthenticated = verifySignature.verify(receivedSignature);

  if (isAuthenticated) {
   System.out.println("Data is authenticated");
  } else {
   System.out.println("Data is not from expected sender");
  }

 }
}

PublicKeyUtil.java

FOR FULL PORGRAM and lab manual
CONTACT:
CELL:9789697608
           9566627095
EMAIL:hitechguil@gmail.com 
SignatueUtil.java


FOR FULL PORGRAM and lab manual
CONTACT:
CELL:9789697608
           9566627095
EMAIL:hitechguil@gmail.com

OUTPUT:
PUBLIC KEY


SIGNATURE


No comments:

Post a Comment

Flag Counter