Question

Write a java program to implement the RSA public-key cryptosystem.

Write a java program to implement the RSA public-key cryptosystem.

Homework Answers

Answer #1

Answer:

package com.sanfoundry.setandstring;

import java.io.DataInputStream;

import java.io.IOException;

import java.math.BigInteger;

import java.util.Random;

public class RSA

{

private BigInteger p;

  private BigInteger q;

  private BigInteger N;

  private BigInteger phi;

  private BigInteger e;

  private BigInteger d;

private int bitlength = 1024;

private Random r;

public RSA()

{

r = new Random();

p = BigInteger.probablePrime(bitlength, r);

q = BigInteger.probablePrime(bitlength, r);

N = p.multiply(q);

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

e = BigInteger.probablePrime(bitlength / 2, r);

while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0)

{

e.add(BigInteger.ONE);

}

d = e.modInverse(phi);

}

public RSA(BigInteger e, BigInteger d, BigInteger N)

{

this.e = e;

  this.d = d;

  this.N = N;

}

@SuppressWarnings("deprecation")

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

{

RSA rsa = new RSA();

DataInputStream in = new DataInputStream(System.in);

String teststring;

System.out.println("Enter the plain text:");

teststring = in.readLine();

System.out.println("Encrypting String: " + teststring);

  System.out.println("String in Bytes: " + bytesToString(teststring.getBytes()));

//encrypt

byte[ ] encrypted = rsa.encrypt(teststring.getBytes());

//decrypt

byte[ ] decrypted = rsa.decrypt(encrypted);

  System.out.println(" Decrypting Bytes: " + bytesToString(decrypted));

  System.out.println(" Decrypting String: " + new String(decrypted));

}

private static String bytesToString(byte[ ] encrypted)

{

String test = " ";

for (byte b : encrypted)

{

test += Byte.toString(b);

}

return test;

}

//Encrypt message

public byte[ ] encrypt(byte[ ] message)

{

return (new BigInteger(message)).modPow(e, N).toByteArray();

}

//Decrypt message

public byte[ ] decrypt(byte[ ] message)

{

return (new BigInteger(message)).modPow(d, N).toByteArray();

}

}

OUTPUT:

$ javac RSA.java

$ java RSA

Enter the plain text:

Sanfoundry

Encrypting String: Sanfoundry

String in Bytes: 8397110102111117110100114121

Decrypting Bytes: 8397110102111117110100114121

Decrypted String: Sanfoundry

  

  

  

Know the answer?
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
Similar Questions
For solving the problems, you are required to use the following formalization of the RSA public-key...
For solving the problems, you are required to use the following formalization of the RSA public-key cryptosystem. In the RSA public-key cryptosystem, each participants creates his public key and secret key according to the following steps: ·       Select two very large prime number p and q. The number of bits needed to represent p and q might be 1024. ·       Compute                n = pq                           (n) = (p – 1) (q – 1). The formula for (n) is owing to...
Suppose your RSA Public-key factors are p =6323 and q = 2833, and the public exponent...
Suppose your RSA Public-key factors are p =6323 and q = 2833, and the public exponent e is 31. Suppose you were sent the Ciphertext 6627708. Write a program that takes the above parameters as input and implements the RSA decryption function to recover the plaintext. IN PYTHON
Let p=3 and q=17 and let an RSA public-key cryptosystem be given. 1. Why is the...
Let p=3 and q=17 and let an RSA public-key cryptosystem be given. 1. Why is the number 8 not a valid encryption-key? 2. We encrypt the number M=8 with the help of the encryption-key e=3. Why is the encrypted message C=2? 3. Why is the decryption key d for the encryption-key, (e=3), equal to 11? https://en.wikipedia.org/wiki/RSA_(cryptosystem)#Encryption
In an RSA system, the public key of a given user is e = 31, n...
In an RSA system, the public key of a given user is e = 31, n = 3599. What is the private key of this user?
This is a number theory problem Use RSA encryption to encrypt "PUBLIC KEY CRYPTOGRAPHY" via the...
This is a number theory problem Use RSA encryption to encrypt "PUBLIC KEY CRYPTOGRAPHY" via the key (13,2537)
Bob has an RSA public key of (n, e) = (1363, 87) (a) What is Bob’s...
Bob has an RSA public key of (n, e) = (1363, 87) (a) What is Bob’s private key? (b) Bob receives the ciphertext which has been encrypted with his public key 893, 1265, 406, 171, 980, 1040, 12, 1152, 573 Decrypt the message. (You can use an appropriate package such as Matlab or Wolfram Alpha to do the calculations)
Write the program in java Implement a class Product. Create instance variables to store product name...
Write the program in java Implement a class Product. Create instance variables to store product name and price and supply the values through constructor. For example new Product(“Toaster’, 29.95). Create methods, getName, getPrice. Write a method productPrinter that prints the product name and its price after reducing it by $5. Create a main class and necessary constructs in the main class to run the Product class.
Write code in java Implement a method to build an AVL tree out of a sorted...
Write code in java Implement a method to build an AVL tree out of a sorted (ascending order) array of unique integers, with the fastest possible big O running time. You may implement private helper methods as necessary. If your code builds a tree that is other than an AVL tree, you will not get any credit. If your code builds an AVL tree, but is not the fastest big O implementation, you will get at most 12 points. You...
Write Java program Lab51.java which takes in a string from the user, converts it to an...
Write Java program Lab51.java which takes in a string from the user, converts it to an array of characters (char[] word) and calls the method: public static int countVowels(char[]) which returns the number of vowels in word. (You have to write countVowels(char[]) ).
(i) What are the public and private keys for RSA cryptosystem with p = 3 and...
(i) What are the public and private keys for RSA cryptosystem with p = 3 and q = 7 and 3<e<11. Answer: (ii) In Z6 What is the value of 4⊘5? (iii) (Chinese Remainder Theorem) Find the value of x where: x ≡ 2 mod 3 x ≡ 3 mod 5 x ≡ 2 mod 7 (Note:All necessary steps are required to show the result)
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT