Question

In: Computer Science

This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA...

This is an intro to java question. Please answer with pseudocode and code.

Problem 2: RSA Public Key (10 points) (Cyber Security)

RSA is an asymmetric encryption scheme, where a public key is used to encrypt data and a different, private key decrypts that data. RSA public/private keys are generated from two prime numbers, typically very large ones. The idea behind RSA is based on the fact that its difficult to factorize very large integers. RSA public key generation is done in part by using the composite number (n) between two prime numbers (p,q), and some other coprime number (e) which is not a divisor of n. Determine if two seed numbers and the given e are valid for generating a RSA key, and if so, calculate n, the basis for generating private/public keys.

Facts RSA Public/Private key generation requires:

● p, one of the given numbers, it must be prime.

● q, one of the given numbers, it must be prime.

● n is a composite number where p * q

● totient(n) = (p - 1) * ( q - 1)

● e is a coprime to n (i.e. not divisible), and must be between 1 < totient(n).

● Consider using long data types instead of int

Input

First line represents the number of test cases. Each line there after, consists of three positive integers. The first integer represents the p terms, the second integer represents the q term, the third integer represents the e term.

Output

For each pair of integers, determine if they are not valid for key generation. if not, either print "Invalid n for RSA Key!" or "Invalid e for RSA Key!" depending on which is not valid. Otherwise, print "RSA Public Key: n=%d e=%d" with n and e respectively.

Sample Input

4

5 3 7

33 7 5

131101 524269 17

5 3 17

Sample Output

RSA Public Key: n=15 e=7

Invalid n for RSA Key!

RSA Public Key: n=68732190169 e=17

Invalid e for RSA Key!

Solutions

Expert Solution

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.IOException;

public class Task4 {

public static boolean isPrime(int n) {

if (n == 1)

return false;

for (long i = 2; i * i < (long)n; i++) {

if (n % i == 0)

return false;

}

return true;

}

public static String checkValidKeys(int p, int q, int e) {

if (!isPrime(p)||!isPrime(q)) {

return "Invalid n for RSA Key!";

}

long n = ((long)p) * q;

long totientN = ((long)(p - 1)) * (q - 1);

if (e <= 1 || e >= totientN || n % e == 0 || e % n == 0) {

return "Invalid e for RSA Key!";

}

return "RSA Public Key: n=" + n + ", e=" + e;

}

public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int T = Integer.parseInt(br.readLine());

String message[] = new String[T];

for (int i = 0; i < T; i++) {

String line = br.readLine();

String words[] = line.split(" ");

int p = Integer.parseInt(words[0]);

int q = Integer.parseInt(words[1]);

int e = Integer.parseInt(words[2]);

message[i] = checkValidKeys(p, q, e);

}

br.close();

for (int i = 0; i < T; i++) {

System.out.println(message[i]);

}

}

}


Related Solutions

This is an Intro to Java Question, Please respond with code and pseudocode. Problem 3: RSA...
This is an Intro to Java Question, Please respond with code and pseudocode. Problem 3: RSA Private Key (10 points) (Cyber Security) In the RSA algorithm, encrypting and decrypting a message is done using a pair of numbers that are multiplicative inverses with respect to a carefully selected modulus. In this task, you must calculate the modular multiplicative inverse for given set of values. What is the Modular Multiplicative Inverse? In mathematics, each operation typically has an inverse. For example,...
This is an Intro to java question. Please provide code and pseudocode for better understanding. Problem...
This is an Intro to java question. Please provide code and pseudocode for better understanding. Problem 4: Player Move Overworld (10 points) (Game Development) You're the lead programmer for an indie game studio making a retro-style game called Zeldar. You've been tasked to implement the player movement. The game is top-down, with the overworld modeled as a 2d grid. The player's location is tracked by x,y values correlating to its row and column positions within that grid. Given the current...
Intro to java Problem, Please provide code and Pseudocode. Not able to compile correct numbers. Problem...
Intro to java Problem, Please provide code and Pseudocode. Not able to compile correct numbers. Problem 7: Simple Calculator (10 points) (General) Calculators represent the most basic, general-purpose of computing machines. Your task is to reduce your highly capable computer down into a simple calculator. You will have to parse a given mathematical expression, and display its result. Your calculator must support addition (+), subtraction (-), multiplication (*), division (/), modulus(%), and exponentiation (**). Facts ● Mathematical expressions in the...
Intro to Java Question. Please Provide code and pseudocode for understanding. Not receiving correct results currently....
Intro to Java Question. Please Provide code and pseudocode for understanding. Not receiving correct results currently. Problem 5: Player Move Dungeon (10 points) (Game Development) You're the lead programmer at a AAA studio making a sequel to the big hit game, Zeldar 2. You've been challenged to implement player movement in dungeons. The game is top-down, with dungeons modeled as a 2d grid with walls at the edges. The player's location is tracked by x,y values correlating to its row...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a method to test if a LinkedList<Long> has Long values that form a Fibonacci sequence from the beginning to the end and return true if it is and false otherwise. A sequence of values is Fibonnaci if every third value is equal to sum of the previous two. Eg., 3,4,7,11,18,29 is a Fibonacci sequence whereas 1,2,3,4 is not, because 2+3 is not equal to 4....
(I AM IN A INTRO TO PROGRAMMING, PLEASE USE SIMPLE PSEUDOCODE IF POSSIBLE) Create pseudocode for...
(I AM IN A INTRO TO PROGRAMMING, PLEASE USE SIMPLE PSEUDOCODE IF POSSIBLE) Create pseudocode for a program for Hansel's Housecleaning Service. The program prompts the user for a customer's last name only. While the last name is not “zzz” your program will ask for the number of bathrooms and the number of other rooms to be cleaned and display the cleaning charge. You should use a sentinel-controlled while loop in your main loop logic. A customer name of “zzz”...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import java.util.Scanner; ;//import Scanner to take input from user public class TestScore {    @SuppressWarnings("resource")    public static void main(String[] args) throws ScoreException {//main method may throw Score exception        int [] arr = new int [5]; //creating an integer array for student id        arr[0] = 20025; //assigning id for each student        arr[1] = 20026;        arr[2] = 20027;...
How Heapify is done (theory, pseudocode, and examples) the examples used Java code please (in your...
How Heapify is done (theory, pseudocode, and examples) the examples used Java code please (in your own words)
Write in Java. Separate code for each question. The answer to the first question should NOT...
Write in Java. Separate code for each question. The answer to the first question should NOT be comparable. 1. Write a class to represent a AlternativeEnergyCar. Select the fields and methods that fit the modeling of an alternative energy car. Make sure to include code for the constructors, set/get methods, a toString() method. 2. Inheritance – Create two abstract subclasses of AECar, one for Electric cars and one for Altfuel cars. Next create four additional subclasses., two for types of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT