Rsa

 import java.util.*;


public class RSAKeys {

    public static int gcd(int a, int b) { return b==0 ? a : gcd(b, a%b); }


    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter p: "); int p = sc.nextInt();

        System.out.print("Enter q: "); int q = sc.nextInt();


        int n = p * q;

        int phi = (p-1) * (q-1);


        int e = 2;

        while (gcd(e, phi) != 1) e++; // choose smallest e that is coprime


        int d = 1;

        while ((d * e) % phi != 1) d++; // find modular inverse


        System.out.println("Public Key (e, n): (" + e + ", " + n + ")");

        System.out.println("Private Key (d, n): (" + d + ", " + n + ")");

    }

}


Comments

Popular posts from this blog

1

Vigenere