Demonstrating Digital Signature in Java

import java.util.*;
import java.math.BigInteger;
public class ds {
	public static void main(String[] args) {
		Scanner in = new Scanner(;
		BigInteger p,q,phi,p1,q1,e,m,m2,N,t,d = new BigInteger("0"),i = new BigInteger("1");
		System.out.println("Enter values for p and q");
		p = in.nextBigInteger();
		q = in.nextBigInteger();
		N = p.multiply(q);
		p1 = p.subtract(BigInteger.ONE);
		q1 = q.subtract(BigInteger.ONE);
		phi = p1.multiply(q1);
		System.out.println("At Sender's end");
		System.out.println("Enter senders's public key");
		e = in.nextBigInteger();
		d = e.modInverse(phi);
		System.out.println("Enter Message");
		m = in.nextBigInteger();
		t = m.modPow(d,N);
		System.out.println("At Receiver's end");
		System.out.println("The received message with digital signature is: " + t);
		m2 = t.modPow(e,N);
		System.out.println("The decrypted message is: " + m2);

/* Output

Enter values for p and q
17 11
At Sender's end
Enter senders's public key
Enter Message
At Receiver's end
The received message with digital signature is: 88
The decrypted message is: 11


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.