-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockChainingMode.java
More file actions
46 lines (40 loc) · 1.38 KB
/
BlockChainingMode.java
File metadata and controls
46 lines (40 loc) · 1.38 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
package nth.api.cryptography.twofish;
import java.util.Arrays;
/**
*
* @author Hau Trung Nguyen
*/
public abstract class BlockChainingMode {
protected abstract int[] encrypt(int[] block, int[] key);
protected abstract int[] decrypt(int[] block, int[] key);
public int[][] encrypt(int[][] p, int[] key, int[] initialVector) {
int[] fi = Arrays.copyOf(initialVector, initialVector.length);
int[][] c = new int[p.length][];
for (int i = 0; i < p.length; i++) {
int[] cypherInput = new int[p[i].length];
for (int j = 0; j < p[i].length; j++) {
cypherInput[j] = fi[j] ^ p[i][j];
}
c[i] = encrypt(cypherInput, key);
for (int j = 0; j < fi.length; j++) {
fi[j] ^= c[i][j];
}
}
return c;
}
public int[][] decrypt(int[][] c, int[] key, int[] initialVector) {
int[] fi = Arrays.copyOf(initialVector, initialVector.length);
int[][] p = new int[c.length][];
for (int i = 0; i < c.length; i++) {
int[] cypherOutput = decrypt(c[i], key);
for (int j = 0; j < cypherOutput.length; j++) {
cypherOutput[j] ^= fi[j];
}
p[i] = cypherOutput;
for (int j = 0; j < fi.length; j++) {
fi[j] ^= c[i][j];
}
}
return p;
}
}