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

Ex. No.3 Digital Signature Standard

The document provides a Java implementation of the Digital Signature Standard (DSS) using the DSA algorithm. It includes key generation, message signing, and signature verification processes, along with the extraction of domain parameters and signature values. The program prompts the user for a message, computes its hash, generates a digital signature, and verifies its authenticity.

Uploaded by

cseaiml251258
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)
3 views2 pages

Ex. No.3 Digital Signature Standard

The document provides a Java implementation of the Digital Signature Standard (DSS) using the DSA algorithm. It includes key generation, message signing, and signature verification processes, along with the extraction of domain parameters and signature values. The program prompts the user for a message, computes its hash, generates a digital signature, and verifies its authenticity.

Uploaded by

cseaiml251258
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

Ex. No.

3 Implementation of Digital Signature Schemes

Program (Digital Signature Standard)


import java.math.BigInteger;
import java.security.*;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
import java.security.spec.DSAParameterSpec;
import java.util.Base64;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class DSSDigitalSignature {
public static void main(String[] args) {
try {
// Generate DSA key pair
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("DSA");
keyPairGen.initialize(1024); // Key size
KeyPair keyPair = keyPairGen.generateKeyPair();
DSAPublicKey publicKey = (DSAPublicKey) keyPair.getPublic();
DSAPrivateKey privateKey = (DSAPrivateKey) keyPair.getPrivate();
// Extract domain parameters
DSAParameterSpec params = new DSAParameterSpec(publicKey.getParams().getP(),
publicKey.getParams().getQ(), publicKey.getParams().getG());
System.out.println("Domain Parameters:");
System.out.println("p: " + params.getP().toString(16));
System.out.println("q: " + params.getQ().toString(16));
System.out.println("g: " + params.getG().toString(16));
// Print private key (x)
System.out.println("Private Key (x): " + privateKey.getX().toString(16));
// Prompt user for message input
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the message to be signed: ");
String message = scanner.nextLine();
scanner.close();
System.out.println("Message to be signed:");
System.out.println(message);
byte[] messageBytes = message.getBytes();
// Compute hash of the message
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] messageHash = md.digest(messageBytes);
System.out.println("Hash of Message (H(m)): " + new BigInteger(1, messageHash).toString(16));
// Generate a random integer k
SecureRandom random = new SecureRandom();
BigInteger k = new BigInteger(params.getQ().bitLength(), random).mod(params.getQ());
System.out.println("Random Integer (k): " + k.toString(16));
// Signing process
Signature signature = Signature.getInstance("SHA256withDSA");
signature.initSign(privateKey);
signature.update(messageBytes);
byte[] digitalSignature = signature.sign();
// Extract r and s values from the signature
BigInteger r = new BigInteger(1, digitalSignature, 0, digitalSignature.length / 2);
BigInteger s = new BigInteger(1, digitalSignature, digitalSignature.length/2, digitalSignature.length/2);
System.out.println("Signature values:");
System.out.println("r: " + r.toString(16));
System.out.println("s: " + s.toString(16));
// Concatenating r and s into a single signature
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(r.toByteArray());
outputStream.write(s.toByteArray());
byte[] combinedSignature = outputStream.toByteArray();
System.out.println("Combined Digital Signature: " + Base64.getEncoder().encodeToString(combinedSignature));
// Verification process
Signature signatureVerify = Signature.getInstance("SHA256withDSA");
signatureVerify.initVerify(publicKey);
signatureVerify.update(messageBytes);
boolean isVerified = signatureVerify.verify(digitalSignature);
System.out.println("Signature Verified: " + isVerified);
} catch (IOException | NoSuchAlgorithmException | InvalidKeyException | SignatureException e)
{
e.printStackTrace();
}
}
}

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