@@ -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
0 commit comments