0% found this document useful (0 votes)
19 views4 pages

Practical 8 Is Tikeshwar d2 59

information security practical

Uploaded by

Tikeshwar Barade
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)
19 views4 pages

Practical 8 Is Tikeshwar d2 59

information security practical

Uploaded by

Tikeshwar Barade
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/ 4

Shri Ramdeobaba College of Engineering & Management Nagpur-13

Department of Computer Application


Session: 2024-2025

Submission for
Course Name: Information Security Lab

Course Code: MCP645-1

Name of the Student: Tikeshwar Omkar Barade

Class Roll No: 59

Semester: 3rd semester mca

Batch: B3

Under the Guidance of


Prof. Manda Ukey

Date of submission:
Practical 8: Write a program to implement public key cryptography RSA algorithm.

Note: a) The two numbers p and q should be prime.

b) Display the public key and private key value.

c) Input value should be in Integer.

d) Encryption and Decryption should be in the same program.

Code:

package Practical8;

import java.math.BigInteger;
import java.util.Scanner;

public class RSA {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter the first prime number (p): ");


int p = sc.nextInt();

System.out.print("Enter the second prime number (q): ");


int q = sc.nextInt();

if (!isPrime(p) || !isPrime(q)) {
System.out.println("Both numbers must be prime.");
return;
}

int n = p * q;
int z = (p - 1) * (q - 1);

System.out.println("The value of n (p * q) = " + n);


System.out.println("The value of z (φ(n)) = " + z);

int e;
for (e = 2; e < z; e++) {
if (gcd(e, z) == 1) {
break;
}
}
System.out.println("The value of e (public exponent) = " + e);

int d = 0;
for (int i = 1; i <= 9; i++) {
int x = 1 + (i * z);
if (x % e == 0) {
d = x / e;
break;
}
}
System.out.println("The value of d (private exponent) = " + d);

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


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

System.out.print("Enter the message to encrypt (as an integer): ");


int msg = sc.nextInt();

BigInteger M = BigInteger.valueOf(msg);
BigInteger N = BigInteger.valueOf(n);
BigInteger C = M.pow(e).mod(N);
System.out.println("Encrypted message is: " + C);

BigInteger decryptedMsg = C.pow(d).mod(N);


System.out.println("Decrypted message is: " + decryptedMsg);

sc.close();
}

public static int gcd(int a, int b) {


if (b == 0)
return a;
else
return gcd(b, a % b);
}

public static boolean isPrime(int num) {


if (num <= 1) return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) return false;
}
return true;
}
}
Output:

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