0% found this document useful (0 votes)
10 views3 pages

RSA Algorithm Implementation in Java

The document presents a Java implementation of the RSA algorithm, which includes functions for calculating the GCD, generating public and private keys, and encrypting and decrypting messages. Users input two prime numbers, and the program checks their validity before proceeding with key generation and message processing. The encrypted and decrypted messages are displayed as output.

Uploaded by

srushtishinde341
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)
10 views3 pages

RSA Algorithm Implementation in Java

The document presents a Java implementation of the RSA algorithm, which includes functions for calculating the GCD, generating public and private keys, and encrypting and decrypting messages. Users input two prime numbers, and the program checks their validity before proceeding with key generation and message processing. The encrypted and decrypted messages are displayed as output.

Uploaded by

srushtishinde341
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

EXP -06

AIM: Implementation of RSA algorithm.


Code:

import [Link];
import [Link];
import [Link];

public class RSA {

// Function to find GCD


public static BigInteger gcd(BigInteger a, BigInteger b) {
while (![Link]([Link])) {
BigInteger temp = b;
b = [Link](b);
a = temp;
}
return a;
}

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
Random rand = new Random();

// Input two prime numbers


[Link]("Enter a prime number p: ");
BigInteger p = [Link]();
[Link]("Enter a prime number q: ");
BigInteger q = [Link]();

// Prime check
if (![Link](10)) {
[Link]("Error: p is not a prime number. Exiting.");
return;
}
if (![Link](10)) {
[Link]("Error: q is not a prime number. Exiting.");
return;
}

// n = p * q
BigInteger n = [Link](q);

// phi = (p - 1) * (q - 1)
BigInteger phi =
([Link]([Link])).multiply([Link]([Link]));

// Choose e such that 1 < e < phi and gcd(e, phi) = 1


BigInteger e, d;
while (true) {
e = new BigInteger([Link](), rand);
if ([Link]([Link]) > 0 && [Link](phi) < 0
&& gcd(e, phi).equals([Link])) {
d = [Link](phi);
if (![Link](d)) break; // Ensure e != d
}
}

[Link]("\nPublic Key: (" + e + ", " + n + ")");


[Link]("Private Key: (" + d + ", " + n + ")");

// Consume leftover newline


[Link]();
// Input message
[Link]("\nEnter the message to encrypt: ");
String message = [Link]();
char[] chars = [Link]();

// Encrypt each character


BigInteger[] encrypted = new BigInteger[[Link]];
[Link]("Encrypted Message: ");
for (int i = 0; i < [Link]; i++) {
BigInteger m = [Link]((int) chars[i]);
encrypted[i] = [Link](e, n);
[Link](encrypted[i] + " ");
}

// Decrypt
[Link]("\nDecrypted Message: ");
for (BigInteger c : encrypted) {
BigInteger m = [Link](d, n);
char decryptedChar = (char) [Link]();
[Link](decryptedChar);
}
}
}

You might also like