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

CN-5 KN

The document outlines a Java program that implements the RSA encryption algorithm, which is an asymmetric cryptographic method using public and private keys. It details the steps for generating keys, encrypting a message, and decrypting it back to verify successful encryption. The source code provided demonstrates the complete implementation of the RSA algorithm, including key generation and message processing.
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

CN-5 KN

The document outlines a Java program that implements the RSA encryption algorithm, which is an asymmetric cryptographic method using public and private keys. It details the steps for generating keys, encrypting a message, and decrypting it back to verify successful encryption. The source code provided demonstrates the complete implementation of the RSA algorithm, including key generation and message processing.
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
You are on page 1/ 3

URK22CS7106

EX N0:5 Implement Asymmetric Cipher


DATE
AIM:To develop a Java program to implement the RSA encryption algorithm

DESCRIPTION:
RSA is an algorithm used by modern computers to encrypt and decrypt messages. It is
an asymmetric cryptographic algorithm. Asymmetric means that there are two different
keys. This is also called public key cryptography, because one of them can be given to
everyone.
A basic principle behind RSA is the observation that it is practical to find three very
large positive integers e, d and n such that with modular exponentiation for all integer
m: (m^e ) d = m (mod n)
The public key is represented by the integers n and e; and, the private key, by the
integer d. m represents the message. RSA involves a public key and a private key. The
public key can be known by everyone and is used for encrypting messages. The
intention is that messages encrypted with the public key can only be decrypted in a
reasonable amount of time using the private key.

ALGORITHM:
STEP-1: Select two co-prime numbers as p andq.
STEP-2: Compute n as the product of p and q.
STEP-3: Compute (p-1)*(q-1) and store it in z.
STEP-4: Select a random prime number e that is less than that of z. STEP-5: Compute
the private key, d as e * mod-1(z).
STEP-6: The cipher text is computed as message e * mod n.
STEP-7: Decryption is done as cipher d mod n.

SOURCE CODE:
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Scanner;

public class RSA {


private BigInteger p, q, n, phi, e, d;

public RSA(BigInteger p, BigInteger q) {


this.p = p;
this.q = q;
n = p.multiply(q);
phi = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));

e = BigInteger.valueOf(2);
while (e.compareTo(phi) < 0) {
if (e.gcd(phi).equals(BigInteger.ONE)) {
break;
}
e = e.add(BigInteger.ONE);
}
URK22CS7106

d = e.modInverse(phi);
}

public BigInteger getE() { return e; }


public BigInteger getD() { return d; }
public BigInteger getN() { return n; }

public BigInteger encrypt(String message, BigInteger e1, BigInteger n1) {


BigInteger m1 = convertDataToBigInteger(message);
return m1.modPow(e1, n1);
}

public BigInteger decrypt(BigInteger encryptedMessage, BigInteger d1, BigInteger n1) {


return encryptedMessage.modPow(d1, n1);
}

private BigInteger convertDataToBigInteger(String message) {


StringBuilder binaryString = new StringBuilder();
for (char ch : message.toCharArray()) {
String binaryChar = String.format("%08d", new
BigInteger(Integer.toBinaryString(ch)));
binaryString.append(binaryChar);
}
return new BigInteger(binaryString.toString(), 2);
}

private String convertBigIntegerToMessage(BigInteger bigInt) {


String binaryString = bigInt.toString(2);
while (binaryString.length() % 8 != 0) {
binaryString = "0" + binaryString;
}

StringBuilder message = new StringBuilder();


for (int i = 0; i < binaryString.length(); i += 8) {
int charCode = Integer.parseInt(binaryString.substring(i, i + 8), 2);
message.append((char) charCode);
}
return message.toString();
}

public static void main(String[] args) {


SecureRandom secureRandom = new SecureRandom();
BigInteger p = BigInteger.probablePrime(1024, secureRandom);
BigInteger q = BigInteger.probablePrime(1024, secureRandom);

System.out.println("Generated Prime p: " + p);


URK22CS7106

System.out.println("Generated Prime q: " + q);

RSA rsa = new RSA(p, q);

Scanner scanner = new Scanner(System.in);


System.out.print("Enter the message to encrypt: ");
String message = scanner.nextLine();
scanner.close();

BigInteger encryptedMessage = rsa.encrypt(message, rsa.getE(), rsa.getN());


System.out.println("Encrypted Message: " + encryptedMessage);

BigInteger decryptedMessage = rsa.decrypt(encryptedMessage, rsa.getD(), rsa.getN());


System.out.println("Decrypted Message: " +
rsa.convertBigIntegerToMessage(decryptedMessage));
}
}
OUTPUT:

RESULT:
The program generates RSA keys, encrypts a user-input message, and decrypts it back. It
verifies successful encryption and decryption by comparing the original and decrypted
messages.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy