[Java] I am to complete this RSA class to make it work. Complete To Dos also tak
ID: 3717824 • Letter: #
Question
[Java]
I am to complete this RSA class to make it work. Complete To Dos also take input from Input.txt file such as
Input.txt
e= 103687
p= 1413297339818079839
q= 7795673610480062959
message= gravitatio
--------------------------
import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class RSA_assignment
{
static Scanner console = new Scanner (System.in);
public static void main(String [] args) throws FileNotFoundException
{
System.out.println("Q. How to say hello on Java? : A. Hello Java!!!");
System.out.println("Q. How to say hello on C++? : A. Hello C++!!!");
Scanner inFile = new Scanner (new FileReader("Kamilla2.txt"));
long p,q;
long e;
String ggg;
ggg= inFile.next(); //e-
e=inFile.nextInt();
ggg= inFile.next(); //p=
p=inFile.nextLong();
ggg= inFile.next(); //q=
q=inFile.nextLong();
if ((p-1)%e==0||(q-1)%e==0) {System.out.println("I must choose another e from teacher's website");}
else{ System.out.println("gcd(e,(p-1)(q-1))=1");
BigInteger nBI= BobCalculateN(p, q);
BigInteger eBI= new BigInteger(""+e);
System.out.println("p="+p+" q="+q);
BigInteger dBI=BobCalculateD(p, q, e );
System.out.println("dBI="+dBI);
String mStr; //message
long m; //coded value as for a=01,b=02,.. z=26
// if a is first it coded 1, etc.
ggg= inFile.next(); //message=
String mmm=inFile.next();
System.out.println("message="+mmm);
mStr=getCode(mmm);
System.out.println("mStr="+mStr);
m= Long.parseLong(mStr); //work only for 9-10 letters long
BigInteger mBI=new BigInteger(""+m);
String c;
BigInteger cBI = mBI.modPow(eBI,nBI);
// c is encrypted message to post nBI n-to post
System.out.print("n= ");
System.out.print(nBI);
System.out.println();
System.out.print("e= ");
System.out.print(eBI);
System.out.println();
System.out.print("c= ");
System.out.print(cBI);
System.out.println();
System.out.print("m= ");
System.out.print(mBI);
System.out.println();
// PrintWriter outFile = new PrintWriter("Encripted.txt");
// I will get encrypted c from somebody , d and n are mine
BigInteger decryptedmBI=cBI.modPow(dBI, nBI); //decrypted message
// outFile.println(cBI);
// outFile.println(decriptedmBI);
System.out.print("mBI= ");
System.out.println(mBI);
System.out.print("decryptedmBI= ");
System.out.println(decryptedmBI);
String decriptedStr=decryptedmBI.toString();
System.out.println(decriptedStr);
//Print decoded message on meaningful English
PrintLetters(decriptedStr);
inFile.close();
// outFile.close();
}
}
public static long findPrime()
{
//ToDo
long p=0;
return p;
}
public static void PrintLetters(String decriptedStr)
{
//ToDo
}
public static void getCodeOfLetters(String str)
{
//todo
}
public static String getCode(String str)
{
//ToDo
String temp="1";
return temp;
}
public static BigInteger BobCalculateD(long p, long q, long e )
{
//ToDo
BigInteger dBI=new BigInteger(""+0);
return dBI;
}
public static BigInteger BobCalculateN(long p, long q)
{
//ToDo
BigInteger nBI= new BigInteger(""+p);
return nBI;
}
}
The goal is to produce something like this output(Doesn't have to be exact) after reading values from file
e= 103687
p= 1413297339818079839
q= 7795673610480062959
message= gravitatio
n=11017604775781478904665244719208583601
d=5452326099268946397172763409398791927
m==7180122092001200915
c==6365888847651359082866965196518212910
decriptedmBI=7180122092001200915
Explanation / Answer
I had written code for RSA .
As you asked above user has to give the input for e,p,q,message based on them RSA applies and gives the result.
Hope this helps Thankyou.....:)
import java.math.BigInteger;
import java.util.*;
import java.io.*;
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 int blocksize = 256;
//blocksize in byte
private Random r;
public RSA() {
r = new Random();
Scanner in = new Scanner(System.in);
System.out.print("Enter e value:");
e=in.nextBigInteger();
System.out.print("Enter p value:");
p=in.nextBigInteger();
System.out.print("Enter q value:");
q=in.nextBigInteger();
N = p.multiply(q);
phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
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;
}
public static void main (String[] args) throws IOException {
RSA rsa = new RSA();
DataInputStream in=new DataInputStream(System.in);
String teststring ;
System.out.print("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());
System.out.println("Encrypted String in Bytes: " + bytesToString(encrypted));
// decrypt
byte[] decrypted = rsa.decrypt(encrypted);
System.out.println("Decrypted String in Bytes: " + bytesToString(decrypted));
System.out.println("Decrypted 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();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.