encryption working

This commit is contained in:
Zach S-B 2021-11-05 22:22:54 +11:00
parent 926c1a9b39
commit a080fc67b6
3 changed files with 159 additions and 18 deletions

92
Q2.java
View File

@ -1,3 +1,5 @@
import java.math.BigInteger;
public class Q2 { public class Q2 {
//Setup_Request: Hello //Setup_Request: Hello
@ -18,22 +20,80 @@ public class Q2 {
* Opens the client and server in seperate terminals * Opens the client and server in seperate terminals
* @param args * @param args
*/ */
public static void main(String[] args) public static void main(String[] args)
{ {
// try Q2 system = new Q2();
// { system.run();
// Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"java Q2Server\""); }
// Thread.sleep(500); //Just to ensure the server has enough time to open
// Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"java Q2Client\"");
// }
// catch (Exception e)
// {
// e.printStackTrace();
// }
Q2Tools toolbox = new Q2Tools(); public void run()
{
System.out.println(toolbox.fastModularExpon("3785", "8395", "65537")); // try
} // {
// Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"java Q2Server\"");
// Thread.sleep(500); //Just to ensure the server has enough time to open
// Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"java Q2Client\"");
// }
// catch (Exception e)
// {
// e.printStackTrace();
// }
Q2RSA rsa = new Q2RSA();
BigInteger[] publicKey = rsa.getPublicKey();
BigInteger privateKey = rsa.getPrivateKey();
BigInteger result1 = encrypt(BigInteger.valueOf(3436), publicKey);
BigInteger result2 = decrypt(result1, privateKey, publicKey);
}
public BigInteger encrypt(BigInteger X, BigInteger[] publicKey)
{
return fme(X ,publicKey[1],publicKey[0]);
}
public BigInteger decrypt(BigInteger Y, BigInteger privateKey, BigInteger[] publicKey)
{
return fme(Y, privateKey, publicKey[0]);
}
/**
* Performs a fast modular exponation, (base^exponent)%modulus.
* Takes input values as strings containg the numbers
* @param baseVal
* @param exponentVal
* @param modulusVal
* @return
*/
public BigInteger fme(BigInteger baseVal, BigInteger exponentVal, BigInteger modulusVal)
{
if(modulusVal.equals(BigInteger.valueOf(1)))
{
return BigInteger.valueOf(0);
}
BigInteger result = new BigInteger("1");
while(exponentVal.compareTo(BigInteger.valueOf(0))==1)
{
if(exponentVal.and(BigInteger.valueOf(1)).compareTo(BigInteger.valueOf(1))==0)
{
result = result.multiply(baseVal);
result = result.mod(modulusVal);
}
exponentVal = exponentVal.shiftRight(1);
baseVal = (baseVal.multiply(baseVal)).mod(modulusVal);
}
return result;
}
} }

64
Q2RSA.java Normal file
View File

@ -0,0 +1,64 @@
import java.math.BigInteger;
import java.security.SecureRandom;
public class Q2RSA {
private static SecureRandom randomNum = new SecureRandom();
private BigInteger e;
private BigInteger p;
private BigInteger q;
private BigInteger n;
private BigInteger m;
private BigInteger d;
Q2RSA()
{
generateRSA();
}
private void generateRSA()
{
//Public Key:
e = new BigInteger("65537");
//Generate p
p = BigInteger.probablePrime(1024, randomNum);
//Generate q
q = BigInteger.probablePrime(1024, randomNum);
//Generate n
n = p.multiply(q);
//Generate phi n
m = ((p.subtract(BigInteger.valueOf(1))).multiply((q.subtract(BigInteger.valueOf(1)))));
//Generate d (private key)
d = e.modInverse(m);
}
/**
* Returns an array that represents a public key
* 0 - n
* 1 - e
* @return
*/
public BigInteger[] getPublicKey()
{
BigInteger[] publicKey = new BigInteger[2];
publicKey[0] = n;
publicKey[1] = e;
return publicKey;
}
/**
* @return the private key
*/
public BigInteger getPrivateKey()
{
return d;
}
}

View File

@ -1,4 +1,6 @@
import java.math.BigInteger; import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Random;
public class Q2Tools { public class Q2Tools {
@ -9,15 +11,30 @@ public class Q2Tools {
public void generateRSA() public void generateRSA()
{ {
//Public Key:
int e = 65537; BigInteger e = new BigInteger("65537");
//Generate p //Generate p
SecureRandom randomNum = new SecureRandom();
BigInteger p = BigInteger.probablePrime(1024, randomNum);
//Generate q //Generate q
BigInteger q = BigInteger.probablePrime(1024, randomNum);
//Generate n
BigInteger n = p.multiply(q);
//Generate phi n
BigInteger m = ((p.subtract(BigInteger.valueOf(1))).multiply((p.subtract(BigInteger.valueOf(1)))));
System.out.println("E: "+e);
System.out.println("P: "+p);
System.out.println("Q: "+q);
System.out.println("N: "+n);
System.out.println("M: "+m);
} }
/** /**