mirror of
https://github.com/zach-sb/SENG2250_Assignment3.git
synced 2025-11-09 05:07:35 +11:00
encryption working
This commit is contained in:
parent
926c1a9b39
commit
a080fc67b6
66
Q2.java
66
Q2.java
@ -1,3 +1,5 @@
|
|||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
public class Q2 {
|
public class Q2 {
|
||||||
|
|
||||||
//Setup_Request: Hello
|
//Setup_Request: Hello
|
||||||
@ -19,6 +21,12 @@ public class Q2 {
|
|||||||
* @param args
|
* @param args
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
Q2 system = new Q2();
|
||||||
|
system.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run()
|
||||||
{
|
{
|
||||||
// try
|
// try
|
||||||
// {
|
// {
|
||||||
@ -31,9 +39,61 @@ public static void main(String[] args)
|
|||||||
// e.printStackTrace();
|
// e.printStackTrace();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
Q2Tools toolbox = new Q2Tools();
|
Q2RSA rsa = new Q2RSA();
|
||||||
|
BigInteger[] publicKey = rsa.getPublicKey();
|
||||||
|
BigInteger privateKey = rsa.getPrivateKey();
|
||||||
|
|
||||||
System.out.println(toolbox.fastModularExpon("3785", "8395", "65537"));
|
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
64
Q2RSA.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
21
Q2Tools.java
21
Q2Tools.java
@ -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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user