0% found this document useful (0 votes)
2 views2 pages

NS Extra Pgms

The document contains two Java programs: the first calculates the SHA-1 message digest of a given text and outputs the hash, while the second implements the Digital Signature Algorithm (DSA) by generating a key pair, signing a message, and verifying the signature. The SHA-1 hash for the test message is 'eb687fcd19af00a1726493f4a31d95b51d9e125c', and the digital signature for the signed message is displayed in Base64 format. The verification process confirms that the signature is valid.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

NS Extra Pgms

The document contains two Java programs: the first calculates the SHA-1 message digest of a given text and outputs the hash, while the second implements the Digital Signature Algorithm (DSA) by generating a key pair, signing a message, and verifying the signature. The SHA-1 hash for the test message is 'eb687fcd19af00a1726493f4a31d95b51d9e125c', and the digital signature for the signed message is displayed in Base64 format. The verification process confirms that the signature is valid.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1. Calculate the message digest of a text using the SHA-1 algorithm.

Program
package ns;
import java.security.MessageDigest;

public class MDSHA1 {

public static void main(String[] args)throws Exception {


try {
String text = "Hello, this is a test message.";

MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] hashBytes = md.digest(text.getBytes("UTF-8"));

// Convert byte array to hex string


StringBuilder hexString = new StringBuilder();
for (byte b : hashBytes) {
hexString.append(String.format("%02x", b));
}

System.out.println("Original Text: " + text);


System.out.println("SHA-1 Hash: " + hexString.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}

Output:
Original Text: Hello, this is a test message.
SHA-1 Hash: eb687fcd19af00a1726493f4a31d95b51d9e125c
2. Write a Java/C/C++ program to implement Digital Signature Algorithm.
Program
package ns;
import java.security.*;
import java.util.Base64;

public class DSA {


public static void main(String[] args) {
try {
// Step 1: Generate key pair
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
keyGen.initialize(1024); // Key size
KeyPair keyPair = keyGen.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();

// Step 2: Sign a message


String message = "This is a message to be digitally signed";
Signature sign = Signature.getInstance("SHA1withDSA");
sign.initSign(privateKey);
sign.update(message.getBytes());
byte[] digitalSignature = sign.sign();

// Display the signature


System.out.println("Message: " + message);
System.out.println("Digital Signature: " +
Base64.getEncoder().encodeToString(digitalSignature));

// Step 3: Verify the signature


Signature verifySig = Signature.getInstance("SHA1withDSA");
verifySig.initVerify(publicKey);
verifySig.update(message.getBytes());

boolean isVerified = verifySig.verify(digitalSignature);


System.out.println("Signature Verified? " + isVerified);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Output:
Message: This is a message to be digitally signed
Digital Signature:
MCwCFArz8iKBEb2/he9/MMWZLoVTJSSRAhQp+mImX+2yMRka3+5N/KoHXQhrKg==
Signature Verified? true

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy