Skip to content

Commit 5a08087

Browse files
authored
Add files via upload
1 parent 7b656d8 commit 5a08087

File tree

10 files changed

+823
-0
lines changed

10 files changed

+823
-0
lines changed

NSPK.class

3.98 KB
Binary file not shown.

NSPK.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*********************************************************************************************************************************************************
2+
Goldsmiths, University of London
3+
IS53012B/S: Computer Security (2019-20) Coursework Part 2
4+
Part of BSc Computer Science module Computer Security taught by Dr Ida Pu
5+
6+
GROUP MEMBERS:
7+
Mohammed Tahmid - Student ID: 33595286, [email protected]
8+
Dardan Quqalla - Student ID: 33498388, [email protected]
9+
Butrint Termkolli - Student ID: 33551538, [email protected]
10+
11+
NOTES & ASSUMPTIONS:
12+
- N/A
13+
*********************************************************************************************************************************************************/
14+
15+
import java.util.*;
16+
import java.math.*;
17+
18+
public class NSPK {
19+
public static void main(String[] args) {
20+
21+
//Create Alice and Bob persons and a Server
22+
Person alice = new Person("Alice");
23+
Person bob = new Person("Bob");
24+
Server server;
25+
26+
/*
27+
This is a solution for a problem we ran into. In RSA the message should not be larger than the public key of the entity that’s encrypting the message.
28+
In our case, the Server was encrypting the public key of Bob and sending it to Alice and vice-versa.
29+
In some instances, the keys of Alice and Bob were larger than the Servers Public Key and therefore the cipher would not work properly.
30+
We invented a quick work around to this, a simple if statement to check if Alices and Bobs keys are larger than the servers...
31+
...if they are then keep on generating new keys until the keys of both parties are smaller than the server.
32+
*/
33+
while (true) {
34+
server = new Server("Trusted Server", alice, bob); //Create new server with 2 new people
35+
36+
if (server.getPublicKey(bob).get(0) > server.serverGetPublicKey().get(0) ||
37+
server.getPublicKey(bob).get(1) > server.serverGetPublicKey().get(1) ||
38+
server.getPublicKey(bob).get(1) > server.serverGetPublicKey().get(0) ||
39+
server.getPublicKey(bob).get(0) > server.serverGetPublicKey().get(1) ||
40+
server.getPublicKey(alice).get(0) > server.serverGetPublicKey().get(0) ||
41+
server.getPublicKey(alice).get(1) > server.serverGetPublicKey().get(1) ||
42+
server.getPublicKey(alice).get(1) > server.serverGetPublicKey().get(0) ||
43+
server.getPublicKey(alice).get(0) > server.serverGetPublicKey().get(1)) {
44+
45+
bob.generateNewKeys();
46+
alice.generateNewKeys();
47+
} else {
48+
break;
49+
}
50+
}
51+
52+
/*
53+
Begin NSPK demo.
54+
These print statements are pretty self explanatory.
55+
*/
56+
System.out.println("-----------------------------------------------------------------------");
57+
System.out.println("| FOR DEMO PURPOSES WE PRINT OUT EVERYONES PUBLIC KEYS AND NONCES |");
58+
System.out.println("-----------------------------------------------------------------------");
59+
60+
System.out.println("Alices Public Key: " + server.getPublicKey(alice)); //Get Alice public key from server
61+
System.out.println("Bobs Public Key: " + server.getPublicKey(bob)); //Get Bob public key from server
62+
System.out.println("Servers Public Key: " + server.serverGetPublicKey()); //Get Server public key from server
63+
System.out.println("Alices Public Nonce: " + alice.getNonce()); //Get Alices unencrypted nonce from demonstration purposes
64+
System.out.println("Bobs Nonce: " + bob.getNonce()); //Get Bobs unencrypted nonce from demonstration purposes
65+
System.out.println("-----------------------------------------------------------------------");
66+
67+
68+
System.out.println("| PROTOCOL RUN |");
69+
System.out.println("-----------------------------------------------------------------------");
70+
71+
ArrayList<BigInteger> bobKeyFromServerToAlice = server.requestKey(bob); //Server sends back Bobs public key encrypted with its private key to Alice
72+
ArrayList<BigInteger> aliceDecryptsBobsPublicKey = alice.decryptKeyFromServer(bobKeyFromServerToAlice, server); //Alice decrypts the servers message by using its public key to get Bobs key
73+
BigInteger alicesEncryptedNonce = alice.sendEncryptedNonce(bob, aliceDecryptsBobsPublicKey, alice.getNonce()); //Alice prepares to send her nonce to Bob encrypted with his public key
74+
75+
System.out.println("1) Alices requests Bob public key from the server");
76+
System.out.println("2) Server sends back Bobs public key encrypted with the Servers private key " + bobKeyFromServerToAlice);
77+
System.out.println("---> Alice knows the servers public key so can decrypt the message to get Bobs public key " + aliceDecryptsBobsPublicKey + "\n");
78+
79+
System.out.println("3) Alice sends her nonce encrypted with Bobs public key to Bob: " + alicesEncryptedNonce);
80+
81+
BigInteger alicesNonceDecryptedByBob = bob.decryptNonce(alicesEncryptedNonce); //Bob decrypts Alices nonce using his private key
82+
System.out.println("---> On receipt, Bob decrypts Alices nonce with his private key: " + alicesNonceDecryptedByBob + "\n");
83+
System.out.println("4) Bob requests Alices public key from the server");
84+
ArrayList<BigInteger> alicesKeyFromServerToBob = server.requestKey(alice); //Server sends back Alices public key encrypted with its private key to Bob
85+
86+
System.out.println("5) Server sends back Alices public key encrypted with the Servers private key " + alicesKeyFromServerToBob);
87+
ArrayList<BigInteger> bobDecryptsAlicesPublicKey = bob.decryptKeyFromServer(alicesKeyFromServerToBob, server); //Bob decrypts the servers message by using its public key to get Alices key
88+
System.out.println("---> Bob knows the servers public key so can decrypt the message to get Alices public key " + bobDecryptsAlicesPublicKey + "\n");
89+
BigInteger bobsEncryptedNonce = bob.sendEncryptedNonce(alice, bobDecryptsAlicesPublicKey, bob.getNonce()); //Bob prepares to send his nonce to Alice encrypted with her public key
90+
System.out.println("---> Bobs encrypts his nonce with Alices public key, so only she can decrypt it " + bobsEncryptedNonce);
91+
92+
System.out.println("6) Bob sends one final message to Alice with her nonce and his encrypted nonce");
93+
ArrayList<BigInteger> bobSendsBothNoncesToAlice = bob.finalSend(alice, bobsEncryptedNonce, alicesNonceDecryptedByBob);
94+
System.out.println("---> Alices opens the message and finds her orignal nonce (which was decrypted by Bob) and she decrypts Bobs nonce using her private key " + bobSendsBothNoncesToAlice + "\n");
95+
96+
System.out.println("7) Alice sends one final message to Bob containing his decrypted nonce encrypted with his public key, proving that she decrypted it");
97+
BigInteger aliceSendsBobHisNonce = alice.sendEncryptedNonce(bob, aliceDecryptsBobsPublicKey, bobSendsBothNoncesToAlice.get(0).longValue());
98+
System.out.println("---> The encrypted nonce Alice sends to Bob is " + aliceSendsBobHisNonce);
99+
System.out.println("---> Bob decrypts it with his private key to find " + bob.decryptNonce(aliceSendsBobHisNonce) + " his orignal nonce.");
100+
}
101+
102+
}

Person.class

2.23 KB
Binary file not shown.

Person.java

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*********************************************************************************************************************************************************
2+
Goldsmiths, University of London
3+
IS53012B/S: Computer Security (2019-20) Coursework Part 2
4+
Part of BSc Computer Science module Computer Security taught by Dr Ida Pu
5+
6+
GROUP MEMBERS:
7+
Mohammed Tahmid - Student ID: 33595286, [email protected]
8+
Dardan Quqalla - Student ID: 33498388, [email protected]
9+
Butrint Termkolli - Student ID: 33551538, [email protected]
10+
*********************************************************************************************************************************************************/
11+
12+
import java.util.*;
13+
import java.math.*;
14+
15+
public class Person {
16+
private String name; //Variable to store person name
17+
long nonce; //Variable to store person nonce
18+
RSA_Helper rsa; //Each person will need RSA public private keys
19+
20+
/*
21+
Constructor to instantiate a person
22+
It takes 1 parameter - name of the person
23+
*/
24+
public Person(String name) {
25+
this.name = name; //Assigns person name to variable
26+
this.rsa = new RSA_Helper(); //Creates new RSA instance for person, this gives us a public and private key for the person
27+
28+
/*
29+
Generates a random nonce for the person between 1 and 50
30+
*/
31+
Random rand = new Random(); //New random object
32+
nonce = rand.nextInt((50 - 1) + 1) + 1; //Pick random number from 1 to 50 and let it be there nonce
33+
}
34+
35+
/*
36+
This function generates new keys for a person.
37+
This is a solution for a problem we ran into. In RSA the message should not be larger than the public key of the entity that’s encrypting the message.
38+
In our case, the Server was encrypting the public key of Bob and sending it to Alice and vice-versa.
39+
In some instances, the keys of Alice and Bob were larger than the Servers Public Key and therefore the cipher would not work properly.
40+
We invented a quick work around to this, a simple if statement to check if Alices and Bobs keys are larger than the servers...
41+
...if they are then keep on generating new keys until the keys of both parties are smaller than the server.
42+
43+
*/
44+
public void generateNewKeys() {
45+
this.rsa = new RSA_Helper();
46+
}
47+
48+
/*
49+
Getters
50+
*/
51+
public long getNonce() {
52+
return nonce; //Returns the persons nonce
53+
}
54+
55+
public String getName() {
56+
return name; //Returns the persons name
57+
}
58+
59+
public long getE(){
60+
return rsa.getE(); //Returns the persons public key (e)
61+
}
62+
63+
public long getN(){
64+
return rsa.getN(); //Returns the persons public key (n)
65+
}
66+
67+
private long getD() {
68+
return rsa.getD(); //Returns the persons private key (d) - note method is private
69+
}
70+
71+
/*
72+
This function is used after a person has reqested another persons public key and the
73+
server has responded back with a encrypted version of the persons public key.
74+
The recipient will need to decrypt the key using the servers public key to retrive the
75+
other persons actual public key.
76+
Function takes 2 arguments - the encrypted key and the server it was send from
77+
It returns the decrypted public key
78+
*/
79+
public ArrayList<BigInteger> decryptKeyFromServer(ArrayList<BigInteger> encryptedKey, Server s) {
80+
ArrayList<BigInteger> decryptedKey = new ArrayList<BigInteger>(); //Arraylist to store the decrypted key
81+
82+
//Get the public key of the server
83+
BigInteger serverEValue = BigInteger.valueOf(s.serverGetPublicKey().get(0));
84+
BigInteger serverNValue = BigInteger.valueOf(s.serverGetPublicKey().get(1));
85+
86+
//This is essentially the RSA decryption algorithm happening (c^d mod n)
87+
//Decrypt key using the servers public key
88+
BigInteger result = (encryptedKey.get(0).pow(serverEValue.intValue()).mod(serverNValue));
89+
BigInteger result2 = (encryptedKey.get(1).pow(serverEValue.intValue()).mod(serverNValue));
90+
91+
//Add decrypted key to arraylist
92+
decryptedKey.add(result);
93+
decryptedKey.add(result2);
94+
95+
return decryptedKey; //Return the decrypted public key
96+
}
97+
98+
/*
99+
This function simulates sending an encrypted nonce.
100+
It takes 3 arguments, the receiver (person), the key for the nonce to be encrypted with (this is
101+
going to be the recivers public key) and the nonce
102+
It returns the encrypted nonce
103+
*/
104+
public BigInteger sendEncryptedNonce(Person receiver, ArrayList<BigInteger> key, long nonce) {
105+
//This is essentially the RSA encryption algorithm happening (m^e mod n)
106+
BigInteger result = ((BigInteger.valueOf(nonce)).pow(key.get(0).intValue()).mod(key.get(1))); //Take the nonce and encrypt it with the public key
107+
return result; //Return the encrypted nonce
108+
}
109+
110+
/*
111+
This function decrypts the nonce using the persons private key
112+
It takes 1 argument - the nonce to decrypt
113+
It returns the decrypted nonce
114+
*/
115+
public BigInteger decryptNonce(BigInteger nonce) {
116+
//This is essentially the RSA decryption algorithm happening (c^d mod n)
117+
BigInteger result = (nonce.pow((int)this.getD()).mod(BigInteger.valueOf(this.getN()))); //Take the nonce and decrypt it with your own private key
118+
return result; //Return the decrypted nonce
119+
}
120+
121+
/*
122+
This function simulates step 6 of the protocol where person a sends back the decrypted nonce of the other party alongside
123+
their encrypted nonce using person b's public key.
124+
It takes 3 arguements, the reciver of the message, their own nonce encrypted with the other party's public key and the
125+
nonce of the other party that was decrypted by them.
126+
It returns
127+
*/
128+
public ArrayList<BigInteger> finalSend(Person receiver, BigInteger encryptedNonce, BigInteger nonceOfOtherParty) {
129+
ArrayList<BigInteger> results = new ArrayList<BigInteger>();
130+
131+
results.add(receiver.decryptNonce(encryptedNonce));
132+
results.add(nonceOfOtherParty);
133+
134+
return results;
135+
}
136+
137+
}

RSAAlgorithm.class

6.15 KB
Binary file not shown.

0 commit comments

Comments
 (0)