Question

Write code for RSA encryption package rsa; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class RSA...

Write code for RSA encryption

package rsa;

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

public class RSA {

  

private BigInteger phi;

private BigInteger e;

private BigInteger d;

private BigInteger num;

public static void main(String[] args) {

  

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter the message you would like to encode, using any ASCII characters: ");

String input = keyboard.nextLine();

int[] ASCIIvalues = new int[input.length()];

for (int i = 0; i < input.length(); i++) {

ASCIIvalues[i] = input.charAt(i);

}

String ASCIInumbers = "";

for (int j = 0; j < ASCIIvalues.length; j++) {

ASCIInumbers += ASCIIvalues[j] + " ";

}

System.out.println("-----------------------------------------");

System.out.println();

System.out.println("The ASCII coded sequence is:");

System.out.println();

System.out.println(ASCIInumbers);

System.out.println();

System.out.println("-----------------------------------------");

long P = bigPrime();

long Q = P;

while (Q == P) {

Q = bigPrime();

}

System.out.println();

System.out.println("The two primes are P = " + P + " and Q = " + Q);

System.out.println();

System.out.println("The product of the two primes, P*Q, is the modulus for both the private");

System.out.println("and public key and is thus part of the public domain: " + P * Q);

System.out.println("Something interesting to note about how this algorithm works is that");

System.out.println("while P*Q is public, factoring very large numbers is very difficult");

System.out.println("computationally, so only the person with the knownledge of the");

System.out.println("individual values P and Q, has the tools to derive the private key.");

System.out.println();

System.out.println("-----------------------------------------");

/*

   * Calculate the public key exponent, called E. E is

   * chosen to be an int which is relatively prime to another integer

   * called the totient, usually represented by phi.

   * Calculate phi, which is equal to (P-1)(Q-1). The create a public key

   * exponent, create an integer E that is relatively prime to phi, and

   * also less than phi.

   * calculate phi here, write an algorithm to find E that is relativily prime to phi

   * find E such that gcd(phi,E)=1 and E<phi

   */

System.out.println();

System.out.println("The totient is phi = " + phi);

System.out.println("and");

System.out.println("The public key is E = " + E);

System.out.println();

System.out.println("-----------------------------------------");

/*

   * Now, search for the multiplicative inverse of E mod (P-1)(Q-1). you should be

   *looking for D such that E*D = 1 mod phi. Remember, phi (aka the totient)

   * is equal to (P-1)(Q-1). You need

   * to find what are called the Bezout coefficients of E and phi. The

   * algorithm to get these numbers is called the 'extended euclidean

   * algorithm.' Previously, you used the gcd function from a built-in

   * library to make sure that the GCD of the integer you generated and

   * phi is 1. [ie gcd(phi,E) = 1 ]. Now, because we know gcd(phi,E)=1,

   * there MUST exist coefficients x and y such that x*phi +y*E

   * such that x*phi + y*E = 1. These coefficients are called the Bezout

   * coefficients, and the extended euclidean algorithm we practiced in

   * class finds them. The very important thingto notice about the Bezout

   * coefficient y is that it must also be the multiplicative inverse of E

   * (mod phi). remember: if x*phi + y*E = 1, then y*E = 1 - x*phi, which

   * means y*E = 1 mod phi. In this next section, you should write the

   * first part of the extended Euclidean algorithm, which will confirm

   * that gcd(E,phi)=1, and will also generate a list of remainders and

   * quotients which will be needed to perform the second part of the EA

   * (see pdf on collab). Next, write the second part of the extended

   * euclidean algorithm, which returns the bezout coefficients to find

   * the multiplicative inverse of E (mod phi). We will call that inverse

   * D, and D is called the PRIVATE KEY EXPONENT, and should only be known

   * by the person to whom the message is being sent.

   *code the Euclidean Algorithm below.

   */

  

//Next, begin the second part of the euclidean algorithm, this is where you substitute

//your way back up the algorithm to find the multiplicative inverse of E.

//dont forget to display your results

  

System.out.println("The Bezout Equation for E and phi is given by:");

System.out.println(y + "*" + E + "+" + x + "*" + phi + " = " + (y * E + x * phi));

//you want D to be positive so it can be used as an exponent

//extended Euclidean Algorithm results should be shown here

  

System.out.println("Which means the private key is D = " + D);

System.out.println("D*E mod phi = " + (D * E) % phi);

System.out.println("So the above equation is written as");

System.out.println(D + " * " + E + " mod " + phi + " = " + (D * E) % phi);

System.out.println();

System.out.println("-----------------------------------------");

/*

   * Encrypt the ASCII-coded message using the public key. This

   * is accomplished by taking the ASCII-coded list and raising each

   * component to power of the public key exponent, and dividing modulus

   * PQ. One hint to note: taking the ASCII code and raising to the power

   * E leads to a HUGE number, which will cause an overflow. We must

   * instead make use of the fact that we can divide modulo n after each

   * multiplication. (ie if we take a^b mod n, where a and b are large, we

   * would instead take a*a mod n = c, then c*a mod n = d, then d*a mod

   * n....and so on, until we have performed the multiplication b times.)

   */

//use stringEncrypted as the name of your variable for the list of encrypted

//integers. Perform the calculation to encrypt ASCIIstringEncoded here.

System.out.println();

System.out.println("The ASCII sequence after encrypting with the public key is:");

System.out.println();

System.out.println(stringEncrypted);

System.out.println();

System.out.println("-----------------------------------------");

/*

   * To decrypt the encrypted code, one raises each of the

   * elements in the encrypted string to the private key exponent, and

   * dividing modulus PQ.

   *

   *

   *

   * use the variable name stringDecrypted for the list of decrypted integers.

    * Perform the calculation to decrypt the list stringEncrypted into the list

    * stringDecrypted here.

   */

  

System.out.println();

System.out.println("The ASCII sequence after decrypting with the private key is:");

System.out.println();

System.out.println(stringDecrypted);

System.out.println();

System.out.println("-----------------------------------------");

String message = "";

for (int n = 0; n < stringDecrypted.length; n++) {

message = message + (char) stringDecrypted[n];

}

System.out.println();

System.out.println("The decrypted message is:");

System.out.println();

System.out.println(message);

System.out.println();

System.out.println("-----------------------------------------");

  

/*To recap what we have learned, let's consider what has happened here.

The receiver of the message has a private key. The key was derived from a

knowledge of P and Q. The public has knowledge only of the number P*Q (which is

large and difficult to factor), and E, which is the public key. If someone

wants to send a *SECRET* message to the receiver, they use the public key to

encrypt that message. The only (known) way to decrypt is with the private key.

Another cool thing about the public/private keys, is that it allows the

receiver to use his/her private key to digitally "sign" a message. This is

necessary because your public key is known to anyone, which means anyone can

send you a message. If you want a way to securely verify from whom the

message was delivered, you can also use RSA encryption have that person "sign"

the message as well.

*/

//To digitally sign your message, input a 4 digit PIN...

System.out.println("Enter a 4 digit integer 'PIN' : ");

int pin = keyboard.nextInt();

//Now, to verify you are the sender, you will encrypt your pin with your private

//key (D, that only you know)

//Here, you should write code which encrypts the pin number with the private key

//Use the variable PINencrypted for the integer value.

  

  

System.out.println();

System.out.println("The encrypted PIN is: " + pinEncrypted);

System.out.println();

//Then you can tell the receiver, "my PIN is 1234, do not accept any message

//from someone whose PIN does not decrypt to 1234." Then, you simply attach

//your encrypted PIN at the end of the message you send. To decrypt, the

//receiver uses the public key, which everyone knows. If the decrypted PIN

//matches 1234, then the receiver knows the mssage came from you.

//Here you should write your code for decrypting the int PINencrypted. Use the

//variable PINdecrypted

  

  

  

  

System.out.println();

System.out.println("The decrypted PIN is: " + pinDecrypted);

System.out.println();

}

/*

   * Part 2 - Define two very large, random, primes. Do not allow the primes

   * to be larger than 2*10^3 or so, as larger numbers may cause an overflow

   * in python. On the other hand, be sure that the primes are at least as

   * large as 5*10^2. Interestingly, real encryption will be done with primes

   * that are 1024 bits, which are numbers that about 300 (i.e. greater than

   * 10^300) digits long. Call the two primes P and Q. Their product, PQ will

   * be the modulus for both the public and private keys. This number, then,

   * is public information. The individual primes, P and Q, are private.

   *

   */

public static int bigPrime() {

boolean prime = false;

int n = 0;

while (!prime) {

Random rand = new Random();

n = rand.nextInt(500);

n = 2 * (n + 500) + 1;

int sqrtn = (int) Math.pow(n, 0.5) + 1;

for (int i = 3; i < sqrtn; i += 2) {

if (n % i == 0) {

prime = false;

break;

} else {

prime = true;

}

}

}

return n;

}

// Method to compute the GCD of two positive integers.

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

if (b == 0) {

return a;

}

return gcd(b, a % b);

}

}

/*Output:

*

*

*

*

*

*

*

*/

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package com.sanfoundry.setandstring; import java.io.DataInputStream; import java.io. IOException; import java.math.BigInteger

@SuppressWarnings (deprecation) public static void main(String[] args) throws IOException RSA rsanew RSA); DataInputStream

Add a comment
Know the answer?
Add Answer to:
Write code for RSA encryption package rsa; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class RSA...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in Python implement the RSA algorithm for cryptography. Set up: 1.Choose two large...

    Write a program in Python implement the RSA algorithm for cryptography. Set up: 1.Choose two large primes, p and q. (There are a number of sites on-line where you can find large primes.) 2.Compute n = p * q, and Φ = (p-1)(q-1). 3.Select an integer e, with 1 < e < Φ , gcd(e, Φ) = 1. 4.Compute the integer d, 1 < d < Φ such that ed ≡ 1 (mod Φ). The numbers e and d are...

  • Use C++ forehand e receiver creates a public key and a secret key as follows. Generate...

    Use C++ forehand e receiver creates a public key and a secret key as follows. Generate two distinct primes, p andq. Since they can be used to generate the secret key, they must be kept hidden. Let n-pg, phi(n) ((p-1)*(q-1) Select an integer e such that gcd(e, (p-100g-1))-1. The public key is the pair (e,n). This should be distributed widely. Compute d such that d-l(mod (p-1)(q-1). This can be done using the pulverizer. The secret key is the pair (d.n)....

  • Computing RSA by hand. Let p = 13, q = 23, e = 17 be your...

    Computing RSA by hand. Let p = 13, q = 23, e = 17 be your initial parameters. You may use a calculator for this problem, but you should show all intermediate results. Key generation: Compute N and Phi(N). Compute the private key k_p = d = e^-1 mod Phi(N) using the extended Euclidean algorithm. Show all intermediate results. Encryption: Encrypt the message m = 31 by applying the square and multiply algorithm (first, transform the exponent to binary representation)....

  • C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string usi...

    C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string using the RSA algorithm (C90 language only). It can contain ONLY the following libraries: stdio, stdlib, math and string. I want to use the following function prototypes to try and implement things: /*Check whether a given number is prime or not*/ int checkPrime(int n) /*to find gcd*/ int gcd(int a, int h) void Encrypt(); void Decrypt(); int getPublicKeys(); int getPrivateKeys();...

  • For the RSA encryption algorithm , do the following (a) Use p=257,q=337(n=pq=86609),b=(p-1)(q-1)=86016. Gcd(E,b)=1, choose E=17, find...

    For the RSA encryption algorithm , do the following (a) Use p=257,q=337(n=pq=86609),b=(p-1)(q-1)=86016. Gcd(E,b)=1, choose E=17, find D, the number which has to be used for decryption, using Extended Euclidean Algorithm (b) One would like to send the simple message represented by 18537. What is the message which will be sent? (c) Decrypt this encrypted message to recover the original message.

  • Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public...

    Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public class Project2 { //Creating an random class object static Random r = new Random(); public static void main(String[] args) {    char compAns,userAns,ans; int cntUser=0,cntComp=0; /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in);       System.out.println("*************************************"); System.out.println("Prime Number Guessing Game"); System.out.println("Y = Yes , N = No...

  • In a public key encryption system, the values p = 7, q = 11, s =...

    In a public key encryption system, the values p = 7, q = 11, s = 53 are selected. A numerical message x is then encrypted using the function y=x' mod pg. What was the original message x if the received message is y = 67? You may leave your answer in terms of an exponent. Explain why it is necessary in public key encryption to choose s to be relatively prime to (p-1)(9 – 1).

  • 2. Alice is a student in CSE20. Having learned about the RSA cryptosystem in class, she...

    2. Alice is a student in CSE20. Having learned about the RSA cryptosystem in class, she decides to set-up her own public key as follows. She chooses the primes p=563 and q = 383, so that the modulus is N = 21 5629. She also chooses the encryption key e-49. She posts the num- bers N = 215629 and e-49 to her website. Bob, who is in love with Alice, desires to send her messages every hour. To do so,...

  • ***Code to solve*** package functions; import java.math.*; // for BigInteger public class NodeTransitionFunction {    public...

    ***Code to solve*** package functions; import java.math.*; // for BigInteger public class NodeTransitionFunction {    public NodeTransitionFunction(Integer exp, Integer KVal) {        // CONSTUCTOR: Sets the class to calculate f(x) = (x ^ exp) mod KVal        // TODO    }             public Integer apply(Integer val) {        // PRE: -        // POST: Implements f(val)        return null;    }       public BigInteger apply(BigInteger val) {        // PRE:...

  • The Diffie-Hellman public-key encryption algorithm is an alternative key exchange algorithm that is used by protocols...

    The Diffie-Hellman public-key encryption algorithm is an alternative key exchange algorithm that is used by protocols such as IPSec for communicating parties to agree on a shared key. The DH algorithm makes use of a large prime number p and another large number, g that is less than p. Both p and g are made public (so that an attacker would know them). In DH, Alice and Bob each independently choose secret keys, ?? and ??, respectively. Alice then computes...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT