AIM:- WAP to implement Digital Signature scheme using RSA.
import [Link];
import [Link];
import [Link];
public class RSADigitalSignatureWithPrimes {
static BigInteger modExp(BigInteger base, BigInteger exp, BigInteger mod) {
return [Link](exp, mod);
}
static boolean isPrime(BigInteger n) {
return [Link](20);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
SecureRandom rand = new SecureRandom();
[Link]("Enter prime number p: ");
BigInteger p = [Link]();
if (!isPrime(p)) {
[Link]("p is not prime. Exiting...");
return;
}
[Link]("Enter prime number q: ");
BigInteger q = [Link]();
if (!isPrime(q)) {
[Link]("q is not prime. Exiting...");
return;
}
BigInteger n = [Link](q);
BigInteger phi = ([Link]([Link])).multiply([Link]([Link]));
// Dynamically choose e and compute d
BigInteger e, d;
while (true) {
e = new BigInteger([Link](), rand);
if ([Link]([Link]) > 0 && [Link](phi) < 0 &&
[Link](e).equals([Link])) {
d = [Link](phi);
if () {
break; // valid e found
}
}
}
[Link]("\nKeys Generated Successfully!");
[Link]("Public Key (e, n): (" + e + ", " + n + ")");
[Link]("Private Key (d, n): (" + d + ", " + n + ")");
[Link]("\nEnter original message (number): ");
BigInteger message = [Link]();
BigInteger signature = modExp(message, d, n);
[Link]("Generated Digital Signature: " + signature);
[Link]("\nEnter received message (number): ");
BigInteger receivedMessage = [Link]();
[Link]("Enter received signature: ");
BigInteger receivedSignature = [Link]();
BigInteger verified = modExp(receivedSignature, e, n);
[Link]("Decrypted Signature Value: " + verified);
if ([Link](receivedMessage)) {
[Link]("Signature Verified Successfully!");
} else {
[Link]("Signature Verification Failed!");
}
[Link]();
}
}