Practical 8 Is Tikeshwar d2 59
Practical 8 Is Tikeshwar d2 59
Submission for
Course Name: Information Security Lab
Batch: B3
Date of submission:
Practical 8: Write a program to implement public key cryptography RSA algorithm.
Code:
package Practical8;
import java.math.BigInteger;
import java.util.Scanner;
if (!isPrime(p) || !isPrime(q)) {
System.out.println("Both numbers must be prime.");
return;
}
int n = p * q;
int z = (p - 1) * (q - 1);
int e;
for (e = 2; e < z; e++) {
if (gcd(e, z) == 1) {
break;
}
}
System.out.println("The value of e (public exponent) = " + e);
int d = 0;
for (int i = 1; i <= 9; i++) {
int x = 1 + (i * z);
if (x % e == 0) {
d = x / e;
break;
}
}
System.out.println("The value of d (private exponent) = " + d);
BigInteger M = BigInteger.valueOf(msg);
BigInteger N = BigInteger.valueOf(n);
BigInteger C = M.pow(e).mod(N);
System.out.println("Encrypted message is: " + C);
sc.close();
}