CRYPTOGRAPHY AND NETWORK SECURITY LAB (22PC0CS25)
LAB PROGRAM 8:
Write a Java program to implement the RSA algorithm.
HARDWARE & SOFTWARE REQUIREMENTS:
1.Intel based desktop PC, RAM-4GB, HDD-512GB
2.Operating System: Windows/Linux
3.Compiler: JDK 24.0.
Theory:
➢ Developed by Rivest, Shamir, and Adleman
➢ RSA is best and most widely used general public key encryption algorithm.
➢ First published by Rivest, Shamir & Adleman of MIT in 1978 .
➢ The (RSA) has been the most widely accepted and implemented general-purpose approach to
public-key encryption.
➢ Block cipher in which plaintext and ciphertext are integers between 0 to n-1 for some n.
➢ Uses large integers (eg. 1024 bits)
➢ It also uses expression and exponentials.
➢ It is based on exponentiation in a finite (Galois) field over integers modulo a prime
➢ RSA Key Setup:
➢ Each user generates a public/private key pair by:
➢ Selecting two large primes randomly: p, q
➢ Computing their system modulus n=p.q
➢ Calculate ø(n)=(p-1)(q-1)
➢ Selecting encryption key e randomly , such that gcd(e ,ø(n))=1,
➢ Here, 1<e<ø(n), means e is not a factor of ø(n).
➢ Find decryption key d such that e.d=1 mod ø(n) or d.e mod ø(n) = 1 where 0 ≤d ≤n
➢ Get the public encryption key: PU={e,n}
➢ Get the private decryption key: PR={d,n}
➢ RSA Encryption/Decryption:
➢ To encrypt a message M, the sender:
➢ Obtains public key of receiver i.e PU={e,n}
➢ Cipher text: C = M^e mod n, where 0≤M<n
➢ To decrypt the cipher text C the receiver:
➢ Uses his private key i.e. PR={d,n}
➢ Plaintext: M = C^d mod n
CODE:
import java.math.BigInteger;
import java.util.Random;
public class SimpleRSA
{
public static void main(String[] args)
{
// Step 1: Choose two prime numbers
int bitLength = 8; // small for simplicity (use 512+ bits for real use)
Random rand = new Random();
BigInteger p = BigInteger.probablePrime(bitLength, rand);
BigInteger q = BigInteger.probablePrime(bitLength, rand);
// Step 2: Compute n = p * q
BigInteger n = p.multiply(q);
// Step 3: Compute φ(n) = (p - 1) * (q - 1)
BigInteger phi = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
// Step 4: Choose e such that 1 < e < φ(n) and gcd(e, φ(n)) = 1
BigInteger e;
do
{
e = new BigInteger(2 * bitLength, rand);
}
while ((e.compareTo(phi) != -1) || (!e.gcd(phi).equals(BigInteger.ONE)));
// Step 5: Compute d = e^(-1) mod φ(n)
BigInteger d = e.modInverse(phi);
// Display keys
System.out.println("Prime p: " + p);
System.out.println("Prime q: " + q);
System.out.println("n (p*q): " + n);
System.out.println("φ(n): " + phi);
System.out.println("Public key (e, n): (" + e + ", " + n + ")");
System.out.println("Private key (d, n): (" + d + ", " + n + ")");
// Step 6: Encrypt message
BigInteger msg = new BigInteger("12"); // example numeric message
BigInteger cipher = msg.modPow(e, n);
System.out.println("\nOriginal Message: " + msg);
System.out.println("Encrypted Message: " + cipher);
// Step 7: Decrypt message
BigInteger decrypted = cipher.modPow(d, n);
System.out.println("Decrypted Message: " + decrypted);
}
}
OUTPUT: