forked from benaich/JavaDataStructures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoder.java
More file actions
66 lines (59 loc) · 2.08 KB
/
Encoder.java
File metadata and controls
66 lines (59 loc) · 2.08 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
package ECC;
import static ECC.Helpers.toBinary;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Encoder {
private HashMap<Integer, Point> charTable;
public Encoder(HashMap<Integer, Point> charTable) {
this.charTable = charTable;
}
public Matrix encode(String plainText) {
Matrix mMatrix = createMatrix(plainText);
System.out.println("\n2) Convert the list of points to a binary Matrix");
System.out.println(mMatrix);
System.out.println("\n3) Matrix Scrambling");
int w = new BigInteger(ECC.PAD, ECC.getRandom()).intValue();
int[] bits = Helpers.toBinary(ECC.getRandom().nextInt(1024), ECC.PAD * 2);
System.out.println("number of transformations, w = " + w);
System.out.println("Random sequence of bits, Bits = ");
Helpers.print(bits);
int bit, i = 0;
do {
bit = bits[i];
if (bit == 0) {
mMatrix.scramble(true);
} else {
mMatrix.scramble(false);
}
if (i == bits.length - 1) {
i = 0;
}else{
i++;
}
//System.out.println("scrambling...");
System.out.println(mMatrix);
w--;
} while (w > 0);
return mMatrix;
}
private Matrix createMatrix(String plainText) {
List<Point> pList = new ArrayList<>();
for (Character c : plainText.toCharArray()) {
Point p = charTable.get((int) c.charValue());
pList.add(p);
}
System.out.println("\n1) Convert m to a list of Points");
pList.stream().forEach(System.out::print);
System.out.println("");
List<Integer> bList = new ArrayList<>();
for (Point p : pList) {
String str = toBinary(p.getX()) + "" + toBinary(p.getY());
for (int i = 0; i < str.length(); i++) {
bList.add((str.charAt(i) == '0') ? 0 : 1);
}
}
return Helpers.listToMatrix(bList);
}
}