-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDecryption.java
More file actions
154 lines (124 loc) · 4.61 KB
/
Copy pathDecryption.java
File metadata and controls
154 lines (124 loc) · 4.61 KB
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
public class Decryption {
/**
* This method Decrypts the Encrypted Array of numbers
*/
public Exception KeysNotEqualException = new Exception("Invalid Amount of Order and Keys!");
public Exception MethodNotFoundException = new Exception("Encryption method not found, Please give Correct order!");
private float chars[];
private int length;
private boolean isEvenLength;
public Decryption(float[] encrypted_chars) {
this.chars = encrypted_chars;
this.length = this.chars.length;
this.isEvenLength = this.length % 2 == 0;
}
// This methods decrypts the chars array from add method.
public void add(int key) {
for(int i = 0; i < this.length; i++)
this.chars[i] = this.chars[i] - key;
}
// Decryption method for addNextNum method
public void addNextNum(int key) {
if(this.length < 2) {
this.chars[0] -= key;
} else if(this.length == 2) {
this.chars[1] = this.chars[1] - this.chars[0] - key;
} else {
this.chars[this.length - 1] = this.chars[length - 1] - key - this.chars[1];
for(int i = this.length - 2; i >= 0; i--)
this.chars[i] = this.chars[i] - this.chars[i + 1] - key;
}
}
public void multiply(int key) {
int i;
for(i = 0; i < this.length; i++)
this.chars[i] = this.chars[i] / key;
}
public void addMirrorElements(int key) {
int len = (int) this.length / 2;
if(this.isEvenLength) {
subFirstFromLast(len);
} else {
this.chars[len] = this.chars[len] - this.chars[len + 1];
subFirstFromLast(len + 1);
}
for(int i = 0; i < len; i++)
this.chars[i] = this.chars[i] - this.chars[this.length - i - 1] - key;
}
public void addKeyMultipliedMirrorElement(int key) {
int len = (int) this.length / 2;
if(this.isEvenLength) {
subFirstFromLast(len);
} else {
this.chars[len] = this.chars[len] - this.chars[len + 1];
subFirstFromLast(len + 1);
}
for(int i = 0; i < len; i++)
this.chars[i] = this.chars[i] - (this.chars[this.length - i - 1] * key);
}
public void addKeyMultipliedElement(int key) {
int len = (int) this.length / 2;
if(this.isEvenLength) {
subFirstFromLast(len);
} else {
this.chars[len] = this.chars[len] - this.chars[len + 1];
subFirstFromLast(len + 1);
}
for(int i = 0; i < len; i++)
this.chars[i] = (this.chars[i] - this.chars[this.length - i - 1]) / key;
}
public void addKeyMultipliedElementMirror(int key) {
int len = (int) this.length / 2;
if(this.isEvenLength) {
subFirstFromLast(len);
} else {
this.chars[len] = this.chars[len] - this.chars[len + 1];
subFirstFromLast(len + 1);
}
for(int i = 0; i < len; i++)
this.chars[i] = this.chars[i] / key - this.chars[this.length - i - 1];
}
private void subFirstFromLast(int len) {
for(int i = len; i < this.length; i++)
this.chars[i] = this.chars[i] - this.chars[i - len];
}
// Decryption
public void Decrypt(int[] order, int[] keys) throws Exception{
int i, KeyLen, OrderLen;
KeyLen = keys.length;
OrderLen = order.length;
if(OrderLen != KeyLen) {
throw KeysNotEqualException;
} else {
for(i = OrderLen - 1; i >= 0; i--) {
switch(order[i]) {
case 1:
this.add(keys[i]);
break;
case 2:
this.addNextNum(keys[i]);
break;
case 3:
this.multiply(keys[i]);
break;
case 4:
this.addMirrorElements(keys[i]);
break;
case 5:
this.addKeyMultipliedMirrorElement(keys[i]);
break;
case 6:
this.addKeyMultipliedElement(keys[i]);
break;
case 7:
this.addKeyMultipliedElementMirror(keys[i]);
break;
default:
throw MethodNotFoundException;
}
}
}
}
// To return the chars Array
public float[] getChars() { return this.chars; }
}