0% found this document useful (0 votes)
14 views

CSS Experiment No. 5

Uploaded by

onlineearn4648
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

CSS Experiment No. 5

Uploaded by

onlineearn4648
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

EXPERIMENT NO.

5
Subject: CSS Lab Class: T.E. SEM: VI Roll No.:
Aim: Implementation of RSA Algorithm.

Code:
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Scanner;

public class RSA {

private static final int BITS = 1024;

public static void main(String[] args) {


// Step 1: Generate public and private keys
KeyPair keyPair = generateKeyPair(BITS);

// Step 2: Take input from the user


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a message to encrypt: ");
String originalMessage = scanner.nextLine();
System.out.println("Original Message: " + originalMessage);

// Step 3: Example of message encryption and decryption


BigInteger encryptedMessage = encrypt(originalMessage,
keyPair.getPublicKey());
System.out.println("Encrypted Message: " + encryptedMessage);

String decryptedMessage = decrypt(encryptedMessage,


keyPair.getPrivateKey());
System.out.println("Decrypted Message: " + decryptedMessage);
}
static KeyPair generateKeyPair(int bits) {
// Step 1: Choose two distinct prime numbers, p and q.
BigInteger p = getRandomPrime(bits);
BigInteger q = getRandomPrime(bits);

// Step 2: Compute n = p * q
BigInteger n = p.multiply(q);

// Step 3: Compute the totient (Euler's totient function) of n, phi(n)


BigInteger phiN =
p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));

// Step 4: Choose an integer e such that 1 < e < phi(n) and gcd(e, phi(n)) = 1
BigInteger e = BigInteger.valueOf(65537); // Commonly used value for e

// Step 5: Compute d, the modular multiplicative inverse of e (mod phi(n))


BigInteger d = e.modInverse(phiN);

// Public key: (e, n)


// Private key: (d, n)
return new KeyPair(new PublicKey(e, n), new PrivateKey(d, n));
}

static BigInteger getRandomPrime(int bits) {


return BigInteger.probablePrime(bits, new SecureRandom());
}

static BigInteger encrypt(String message, PublicKey publicKey) {


// Convert the message to a BigInteger
BigInteger m = new BigInteger(message.getBytes());

// Apply the public key: c = m^e mod n


return m.modPow(publicKey.getExponent(), publicKey.getModulus());
}

static String decrypt(BigInteger cipherText, PrivateKey privateKey) {


// Apply the private key: m = c^d mod n
BigInteger decryptedMessage =
cipherText.modPow(privateKey.getExponent(), privateKey.getModulus());

// Convert the BigInteger back to a string


return new String(decryptedMessage.toByteArray());
}

static class KeyPair {


private final PublicKey publicKey;
private final PrivateKey privateKey;

public KeyPair(PublicKey publicKey, PrivateKey privateKey) {


this.publicKey = publicKey;
this.privateKey = privateKey;
}

public PublicKey getPublicKey() {


return publicKey;
}

public PrivateKey getPrivateKey() {


return privateKey;
}
}

static class PublicKey {


private final BigInteger exponent;
private final BigInteger modulus;
public PublicKey(BigInteger exponent, BigInteger modulus) {
this.exponent = exponent;
this.modulus = modulus;
}

public BigInteger getExponent() {


return exponent;
}

public BigInteger getModulus() {


return modulus;
}
}

static class PrivateKey {


private final BigInteger exponent;
private final BigInteger modulus;

public PrivateKey(BigInteger exponent, BigInteger modulus) {


this.exponent = exponent;
this.modulus = modulus;
}

public BigInteger getExponent() {


return exponent;
}

public BigInteger getModulus() {


return modulus;
}
}
}
Output:
Enter a message to encrypt: hi
Original Message: hi
Encrypted Message:
29747397176228831227229586564056434676552475757957662455897251385
93288800247993536139874597886427082990903444663679119603880948388
06413235300666646970009543358416395596683041110069573582679186252
78427108386035970605030452547381490968415209738815179776270264174
55424801521866588013431234702780232462059311813609191933843383531
98905406648787406848042563613324825130279074987793255055505172876
78194594549864169356172237008527164161636293477451610588761218508
87459047819100854595751174605735358862872746468404708845270055773
33641114489817168578957732152630296710202660913341586152900514174
5315598516998943301133448679526
Decrypted Message: hi

You might also like