Skip to content

Commit 43ffb32

Browse files
committed
fixing bugs
1 parent f431310 commit 43ffb32

File tree

9 files changed

+65
-100
lines changed

9 files changed

+65
-100
lines changed

ECC/Decoder.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public String decode(Matrix A) {
3030
A.reverseColumnTrasformation(b, c, d, op);
3131
}
3232
System.out.println("sub-key : " + subKey);
33-
System.out.println("Fixing...");
3433
System.out.println(A);
3534
}
3635
return getPlainText(A);

ECC/ECC.java

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ public class ECC {
88

99
public static int PAD = 5;
1010
public static final Random r = new Random();
11-
11+
1212
private HashMap<Point, Integer> pointTable;
1313
private HashMap<Integer, Point> charTable;
1414

1515
private Encoder mEncoder;
1616
private Decoder mDecoder;
1717

18-
public ECC(EllipticCurve c){
18+
public ECC(EllipticCurve c) {
1919
initCodeTable(c);
2020
this.mEncoder = new Encoder(charTable);
2121
this.mDecoder = new Decoder(pointTable);
@@ -38,13 +38,13 @@ private int[] encrypt(String msg, PublicKey key) {
3838
Point sharedSecret = c.multiply(publicKey, k);
3939

4040
Point keyHint = c.multiply(g, k); // key to send
41-
41+
4242
System.out.println("----------------- Encrypt -----------------");
4343
System.out.println("Msg to encrypt : " + msg);
4444
System.out.println("Bob public key : " + publicKey);
4545
System.out.println("Alice private key : " + k);
4646
System.out.println("sharedSecret : " + sharedSecret);
47-
47+
4848
System.out.println("keyHint : " + keyHint);
4949
Matrix mMatrix = mEncoder.encode(msg);
5050
mMatrix.performAddition(Helpers.toBinary(sharedSecret));
@@ -60,13 +60,13 @@ private String decrypt(int[] cipherText, PrivateKey key) {
6060
Point g = c.getBasePoint();
6161
BigInteger privateKey = key.getKey();
6262
BigInteger p = c.getP();
63-
63+
6464
Point keyHint = Point.make(cipherText);
6565
Point sharedSecret = c.multiply(keyHint, privateKey);
66-
66+
6767
System.out.println("decrypt cipher :");
6868
Helpers.print(cipherText);
69-
69+
7070
//get the decypted matrix
7171
Matrix mMatrix = Matrix.make(cipherText);
7272
System.out.println("Matrix before substraction");
@@ -99,34 +99,43 @@ public static KeyPair generateKeyPair(EllipticCurve c) {
9999
new PrivateKey(c, privateKey)
100100
);
101101
}
102-
103-
public final void initCodeTable(EllipticCurve curve){
104-
Point p = curve.multiply(curve.getBasePoint(), 2);
102+
103+
public final void initCodeTable(EllipticCurve curve) {
105104
charTable = new HashMap<>();
106105
pointTable = new HashMap<>();
107-
for (int i = 0; i < 26; i++) {
108-
do{
109-
p = curve.add(p, p);
110-
}while(p.isInfinity());
111-
charTable.put(i + 97, p); // 0 here refers to char 97 witch is a
106+
Point p = curve.getBasePoint();
107+
for (int i = 1; i < 27; i++) {
108+
do {
109+
p = curve.multiply(curve.getBasePoint(), i);
110+
} while (p.isInfinity());
111+
charTable.put(i + 96, p); // 0 here refers to char 97 witch is a
112112
}
113113
//special characters
114-
int[] specialCharAscii = new int[]{39, 44, 46, 58, 59};
115-
for(int i: specialCharAscii){
116-
p = curve.add(p, p);
114+
charTable.put(32, Point.getInfinity()); //space
115+
int[] codeAscii = new int[]{10, 13, 39, 40, 41, 44, 46, 58, 59};
116+
for (int i : codeAscii) {
117+
p = curve.add(p, curve.getBasePoint());
117118
charTable.put(i, p);
118119
}
119-
charTable.put(32, Point.getInfinity()); //space
120+
120121
//populate the points symbol table
121-
for(Integer key : charTable.keySet()){
122+
for (Integer key : charTable.keySet()) {
122123
pointTable.put(charTable.get(key), key);
123124
}
124125
}
125126

127+
public void displayCodeTable() {
128+
System.out.println("------ Code Table -------");
129+
charTable.forEach((cle, val) -> {
130+
System.out.println((char) cle.intValue() + " -> " + val);
131+
});
132+
}
133+
126134
public static void main(String[] args) {
127135
EllipticCurve c = new EllipticCurve(4, 20, 29, new Point(1, 5));
128136
ECC ecc = new ECC(c);
129-
137+
ecc.displayCodeTable();
138+
System.out.println(c);
130139
String msg = "i understood the importance in principle of public key cryptography, but it is all moved much faster than i expected i did not expect it to be a mainstay of advanced communications technology";
131140
msg = "hi there";
132141
// generate pair of keys

ECC/EllipticCurve.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,10 @@ public static void main(String[] args) {
157157
System.out.println(p + " x " + i + " = " + e.multiply(p, i));
158158
}
159159
}
160+
161+
@Override
162+
public String toString() {
163+
return "(EC) " + "y^2 = x^3 + " + a + "x + " + b + "c mod " + p + "\n G = " + basePoint + '}';
164+
}
165+
160166
}

ECC/Encoder.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
import java.util.List;
88

99
public class Encoder {
10+
1011
private HashMap<Integer, Point> charTable;
11-
12+
1213
public Encoder(HashMap<Integer, Point> charTable) {
1314
this.charTable = charTable;
1415
}
15-
16+
1617
public Matrix encode(String plainText) {
1718
Matrix mMatrix = createMatrix(plainText);
1819
System.out.println("Matrix before scrambling : ");
1920
System.out.println(mMatrix);
20-
//int w = new BigInteger(ECC.PAD, ECC.getRandom()).intValue();
21-
int w = 6;
21+
int w = new BigInteger(ECC.PAD, ECC.getRandom()).intValue();
2222
int[] bits = Helpers.toBinary(ECC.getRandom().nextInt(1024), ECC.PAD * 2);
23-
System.out.println("Bits : ");
23+
System.out.println("w : " + w + " Bits :");
2424
Helpers.print(bits);
2525
int bit, i = 0;
2626
do {
@@ -30,8 +30,10 @@ public Matrix encode(String plainText) {
3030
} else {
3131
mMatrix.scramble(false);
3232
}
33-
if (i == bits.length) {
33+
if (i == bits.length - 1) {
3434
i = 0;
35+
}else{
36+
i++;
3537
}
3638
System.out.println("scrambling...");
3739
System.out.println(mMatrix);
@@ -43,7 +45,7 @@ public Matrix encode(String plainText) {
4345
private Matrix createMatrix(String plainText) {
4446
List<Point> pList = new ArrayList<>();
4547
for (Character c : plainText.toCharArray()) {
46-
Point p = charTable.get((int)c.charValue());
48+
Point p = charTable.get((int) c.charValue());
4749
pList.add(p);
4850
}
4951
System.out.print("List of Points : ");

ECC/Helpers.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public class Helpers {
1717

1818
static final File SUB_KEYS_FILE = new File("subkey");
19-
19+
2020
static int[] toBinary(int n, int base) {
2121
final int[] bits = new int[base];
2222
for (int i = 0; i < base; i++) {
@@ -37,14 +37,14 @@ static String[] toBinary(Point p) {
3737
String[] tab = new String[ECC.PAD];
3838
String str = toBinary(p.getX()) + "" + toBinary(p.getY());
3939
for (int i = 0; i < str.length(); i = i + 2) {
40-
tab[i/2] = str.charAt(i) + "" + str.charAt(i + 1);
40+
tab[i / 2] = str.charAt(i) + "" + str.charAt(i + 1);
4141
}
4242
return tab;
4343
}
4444

4545
static Matrix listToMatrix(List<Integer> list) {
4646
int n, m, row, col;
47-
n = ECC.PAD;
47+
n = ECC.PAD;
4848
m = list.size() / (2 * n);
4949
String[][] bits = new String[n][m];
5050
for (int i = 0; i < list.size(); i = i + 2) {
@@ -102,28 +102,28 @@ static void print(String[] tab) {
102102
System.out.println(tab1 + " ");
103103
}
104104
}
105-
105+
106106
static void print(int[] tab) {
107107
byte b;
108-
for(int i : tab){
109-
b = (byte)i;
108+
for (int i : tab) {
109+
b = (byte) i;
110110
System.out.print(b);
111111
}
112112
System.out.println("");
113113
}
114-
114+
115115
static int getNotEqualTo(int a, int limit) {
116116
int b;
117117
do {
118118
b = ECC.getRandom().nextInt(limit);
119119
} while (b == a);
120120
return b;
121121
}
122-
122+
123123
static void main(String[] args) {
124-
for(int a : toBinary(15, 5)){
124+
for (int a : toBinary(15, 5)) {
125125
System.out.print(a);
126126
}
127-
System.out.println(" :"+Character.toString ((char) 32)+":");
127+
System.out.println(" :" + Character.toString((char) 32) + ":");
128128
}
129129
}

ECC/KeyPair.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package ECC;
22

33
public class KeyPair {
4+
45
private PublicKey publicKey;
56
private PrivateKey privateKey;
6-
7+
78
public KeyPair(PublicKey publicKey, PrivateKey privateKey) {
89
this.publicKey = publicKey;
910
this.privateKey = privateKey;
@@ -24,4 +25,4 @@ public PrivateKey getPrivateKey() {
2425
public void setPrivateKey(PrivateKey privateKey) {
2526
this.privateKey = privateKey;
2627
}
27-
}
28+
}

ECC/Matrix.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public static Matrix make(String[][] data) {
4040
public static Matrix make(int[] array) {
4141
int keyLength = 2 * ECC.PAD;
4242
Integer[] data = new Integer[(array.length - keyLength)];
43-
4443
// get the matrix
4544
for (int i = 0; i < data.length; i++) {
4645
data[i] = array[i + keyLength];
@@ -205,9 +204,9 @@ public int[] toArray(String[] key) {
205204
int[] array = new int[countColumns() * countRows() * 2 + keySize];
206205
//append the key first
207206
for (k = 0; k < keySize; k = k + 2) {
208-
String str = key[k/2];
209-
array[k] = Integer.parseInt(str.charAt(0) + "");
210-
array[k + 1] = Integer.parseInt(str.charAt(1)+ "");
207+
String str = key[k / 2];
208+
array[k] = Integer.parseInt(str.charAt(0) + "");
209+
array[k + 1] = Integer.parseInt(str.charAt(1) + "");
211210
}
212211
// then add the data
213212
for (int i = 0; i < countColumns(); i++) {
@@ -229,9 +228,11 @@ public List<Point> toPoints() {
229228
}
230229
int x = Integer.parseInt(pointCode.substring(0, pointCode.length() / 2), 2);
231230
int y = Integer.parseInt(pointCode.substring(pointCode.length() / 2), 2);
232-
if(x==0 && y==0)
231+
if (x == 0 && y == 0) {
233232
list.add(Point.getInfinity());
234-
else list.add(new Point(x, y));
233+
} else {
234+
list.add(new Point(x, y));
235+
}
235236
}
236237
return list;
237238
}

ECC/PrivateKey.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package ECC;
22

3-
import java.io.File;
4-
import java.io.PrintStream;
53
import java.math.BigInteger;
64
import java.nio.charset.StandardCharsets;
75
import java.nio.file.Files;
@@ -60,28 +58,4 @@ public BigInteger getKey() {
6058
public Point getBasePoint() {
6159
return c.getBasePoint();
6260
}
63-
64-
/**
65-
* Save the current key to a *.pri file. TODO: design the representation of
66-
* the key inside a binary file
67-
*
68-
* @param path
69-
*/
70-
public void saveToFile(String path) {
71-
BigInteger a = c.getA();
72-
BigInteger b = c.getB();
73-
BigInteger p = c.getP();
74-
BigInteger g1 = c.getBasePoint().getX();
75-
BigInteger g2 = c.getBasePoint().getY();
76-
try (PrintStream ps = new PrintStream(new File(path))) {
77-
ps.println(a.toString(16));
78-
ps.println(b.toString(16));
79-
ps.println(p.toString(16));
80-
ps.println(g1.toString(16));
81-
ps.println(g2.toString(16));
82-
ps.println(k.toString(16));
83-
ps.close();
84-
} catch (Exception e) {
85-
}
86-
}
8761
}

ECC/PublicKey.java

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ECC;
22

33
import java.io.File;
4-
import java.io.FileWriter;
54
import java.io.PrintStream;
65
import java.math.BigInteger;
76
import java.nio.charset.StandardCharsets;
@@ -62,30 +61,4 @@ public void setKey(Point pK) {
6261
public Point getBasePoint() {
6362
return c.getBasePoint();
6463
}
65-
66-
/**
67-
* Save the current key to a *.pub file.
68-
*
69-
* @param path
70-
*/
71-
public void saveToFile(String path) {
72-
BigInteger a = c.getA();
73-
BigInteger b = c.getB();
74-
BigInteger p = c.getP();
75-
BigInteger g1 = c.getBasePoint().getX();
76-
BigInteger g2 = c.getBasePoint().getY();
77-
BigInteger pKx = pK.getX();
78-
BigInteger pKy = pK.getY();
79-
try (PrintStream ps = new PrintStream(new File(path))) {
80-
ps.println(a.toString(16));
81-
ps.println(b.toString(16));
82-
ps.println(p.toString(16));
83-
ps.println(g1.toString(16));
84-
ps.println(g2.toString(16));
85-
ps.println(pKx.toString(16));
86-
ps.println(pKy.toString(16));
87-
ps.close();
88-
} catch (Exception e) {
89-
}
90-
}
9164
}

0 commit comments

Comments
 (0)