-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProblemSet1B.java
340 lines (296 loc) · 9.55 KB
/
ProblemSet1B.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package ProblemSets;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
public class ProblemSet1B {
public static void main(String[] args) {
Person p1 = new Person("A", 90, 'F', false);
Person p2 = new Person("B", 60, 'M', true);
Person p3 = new Person("C", 30, 'F', true);
Person[] p = {p1, p2, p3};
System.out.println(Filter.seniorFilter(p));
Animal[] animals = {new Dog(), new Cat(), new Cow(), new SiberianCat()};
System.out.println(AnimalConcert.performConcert(animals));
String msg = "red is sus";
Browser b = new Browser();
Server s = new Server();
s.setP(97);
s.setQ(53);
b.establishConnection(s);
BigInteger[] encryptedMsg = b.encryptMessage(msg);
String decryptedMsg = s.decryptMessage(encryptedMsg);
System.out.println(Arrays.toString(s.getPublicKey()));
System.out.println(Arrays.toString(s.getPrivateKey()));
System.out.println(Arrays.toString(encryptedMsg));
System.out.println(decryptedMsg);
}
}
// Question 1: Encapsulation
class Person{
// attributes
private String name;
private int age;
private char gender;
private boolean sharingConsent;
// constructor with arguments
Person(String name, int age, char gender, boolean sharingConsent){
this.name = name;
this.age = age;
this.gender = gender;
this.sharingConsent = sharingConsent;
}
// getter for age
public int getAge(){
return age;
}
// setter for age
public void setAge(int age){
this.age = age;
}
// getter for name
public String getName(){
if (isSharingConsent()){
return name;
}
return "Anonymous";
}
// getter for sharingConsent
public boolean isSharingConsent(){
return sharingConsent;
}
}
class Filter{
static ArrayList<String> seniorFilter(Person[] array){
ArrayList<String> result = new ArrayList<>();
for (Person person : array){
if (person.getAge() >= 60){
result.add(person.getName());
}
}
return result;
}
}
// Question 2: Polymorphism
class Animal{
public String makeSound(){
return "I am just an animal";
}
}
class Dog extends Animal{
@Override
public String makeSound(){
return "Woof";
}
}
class Cat extends Animal{
@Override
public String makeSound(){
return "Meow";
}
}
class Cow extends Animal{
@Override
public String makeSound(){
return "Moo";
}
}
class SiberianCat extends Cat{}
class AnimalConcert{
static String performConcert(Animal[] animals){
StringBuilder result = new StringBuilder();
for (Animal animal : animals){
result.append(animal.makeSound() + ", ");
}
result.deleteCharAt(result.length() - 1);
result.deleteCharAt(result.length() - 1);
return result.toString();
}
}
// Question 3: Base Integer
class BaseInteger{
// constructor that takes in 2 inputs
// representation - a string with digits separated by commas and no spaces "22,43,5"
private String representation;
private int base;
private int[] digits;
private int decimalValue;
BaseInteger(String representation, int base){
this.representation = representation;
this.base = base;
}
private void convertRepresentationToArray(){
ArrayList<Integer> dList = new ArrayList<Integer>();
for (String elem : this.representation.split(",")){
dList.add(Integer.parseInt(elem));
}
this.digits = dList.stream().mapToInt(i -> i).toArray();
}
private void setDecimalValue(){
for (int i = 0; i < this.digits.length; i++){
this.decimalValue += this.digits[i] * (Math.pow(this.base, this.digits.length - i - 1));
}
}
public int getDecimalValue(){
return this.decimalValue;
}
public String getDigitsString(){
String output = "[";
for (int i = 0; i < this.digits.length; i++){
output += String.valueOf(this.digits[i]);
if (i != this.digits.length - 1){
output += ",";
}
else {
output += "]";
}
}
return output;
}
public BaseInteger add(BaseInteger other, int base){
int sum = other.getDecimalValue() + this.getDecimalValue();
return new BaseInteger(convertBase(sum, base), base);
}
private String convertBase(int decimalValue, int base){
String repr = "";
int baseInc = 1;
while (decimalValue > 0){
Integer curr = (decimalValue % (baseInc * base)) / baseInc;
repr = curr.toString() + "," + repr;
decimalValue -= curr * baseInc;
baseInc *= base;
}
return repr.substring(0, repr.length() - 1);
}
private String deleteSpaces(String representation){
return representation.replaceAll(" ","");
}
@Override
public String toString(){
return representation + " Base " + base;
}
}
// Question 4: RSA Algorithm
class Server{
private final int[] publicKey = new int[2];
private final int[] privateKey = new int[2];
private int p;
private int q;
// setter for P
public void setP(int p){
this.p = p;
}
// setter for q
public void setQ(int q){
this.q = q;
}
// getter for public key
public int[] getPublicKey(){
return publicKey;
}
// getter for private key
public int[] getPrivateKey(){
return privateKey;
}
// computeModInverse method
private int computeModInverse(int e, int lambda){
for (int d = 1; d < lambda; d++){
if (((e % lambda) * (d % lambda)) % lambda == 1){
return d;
}
}
return 1;
}
// computeE method
private int computeE(int lambda){
for (int i = lambda - 1; i > 2; i--){
if (lambda % i != 0 && isPrime(i)){
return i;
}
}
return 0;
}
// isPrime method
private boolean isPrime(int a){
for (int i = 2; i < a / 2; i++){
if (a % i == 0){
return false;
}
}
return true;
}
// generatePublicKey method
public void generatePublicPrivateKey(){
// todo: compute modulus --> n = pq
int n = p * q;
// todo: compute lambda
int lambda = lcm(p - 1, q -1);
// todo: compute e
int e = computeE(lambda);
// todo: compute d
int d = computeModInverse(e, lambda);
// todo: set (n, e) as public key
publicKey[0] = n;
publicKey[1] = e;
// todo: set (n, d) as private key
privateKey[0] = n;
privateKey[1] = d;
}
// decryptMessage method --> decryption by server for character m = c^d % n
// where (n, d) are values of private key and c is the encrypted character in integer
public String decryptMessage(BigInteger[] encryptedIntMessage){
BigInteger[] decryptedIntMessage = new BigInteger[encryptedIntMessage.length];
StringBuilder builder = new StringBuilder();
// todo: decrypt each character of the message. HINT: use .modPow from BigInteger
for (int i = 0; i < encryptedIntMessage.length; i++){
decryptedIntMessage[i] = encryptedIntMessage[i].modPow(BigInteger.valueOf(privateKey[1]), BigInteger.valueOf(privateKey[0]));
}
// todo: decrypted character is an ASCII value (integer). Convert to char
for (BigInteger element : decryptedIntMessage){
char c = (char) element.intValue();
builder.append(c);
}
// todo: concatenate characters into string
// todo: return decrypted string message
return builder.toString();
}
// least common multiple method: LCM --> |ab| / gcd(a,b)
public static int lcm(int n1, int n2){
int absNum = Math.abs(n1 * n2);
int den = gcd(n1, n2);
return absNum / den;
}
// greatest common divisor method: GCD --> using Euclids Algorithm
public static int gcd(int a, int b){
if (b == 0){
return a;
}
return gcd(b, a % b);
}
public static void main(String[] args) {
System.out.println(lcm(12, 18));
System.out.println(gcd(2, 6));
}
}
class Browser {
private final int[] publicKey = new int[2];
public void establishConnection(Server s){
// todo: generating a public and private key by the server
s.generatePublicPrivateKey();
// todo: get the public key from the server and use it to set Browser object's public key
publicKey[0] = s.getPublicKey()[0];
publicKey[1] = s.getPublicKey()[1];
}
public BigInteger[] encryptMessage(String message){
BigInteger[] encryptedIntMessage = new BigInteger[message.length()];
// todo: loop through each character of the message, convert to its ASCII value in integer
// todo: encrypt the value, using .modPow method from BigInteger
for (int i = 0; i < message.length(); i++){
char c = message.charAt(i);
BigInteger character = BigInteger.valueOf((int) c);
BigInteger encryptedCharacter = character.modPow(BigInteger.valueOf(publicKey[1]), BigInteger.valueOf(publicKey[0]));
encryptedIntMessage[i] = encryptedCharacter;
}
return encryptedIntMessage;
}
}