17
17
*/
18
18
public class ECC {
19
19
20
- private BigInteger privateKey ; // Private key used for decryption
21
- private ECPoint publicKey ; // Public key used for encryption
22
- private EllipticCurve curve ; // Elliptic curve used in cryptography
23
- private ECPoint basePoint ; // Base point G on the elliptic curve
20
+ private BigInteger privateKey ; // Private key used for decryption
21
+ private ECPoint publicKey ; // Public key used for encryption
22
+ private EllipticCurve curve ; // Elliptic curve used in cryptography
23
+ private ECPoint basePoint ; // Base point G on the elliptic curve
24
+
25
+ public ECC (int bits ) {
26
+ generateKeys (bits ); // Generates public-private key pair
27
+ }
24
28
25
29
public EllipticCurve getCurve () {
26
30
return curve ; // Returns the elliptic curve
27
31
}
28
32
29
- public ECC ( int bits ) {
30
- generateKeys ( bits ); // Generates public-private key pair
33
+ public void setCurve ( EllipticCurve curve ) {
34
+ this . curve = curve ;
31
35
}
32
36
33
37
// Getter and Setter for private key
@@ -39,29 +43,25 @@ public void setPrivateKey(BigInteger privateKey) {
39
43
this .privateKey = privateKey ;
40
44
}
41
45
42
- public void setCurve (EllipticCurve curve ) {
43
- this .curve = curve ;
44
- }
45
-
46
46
/**
47
47
* Encrypts the message using the public key.
48
48
* The message is transformed into an ECPoint and encrypted with elliptic curve operations.
49
49
*
50
50
* @param message The plain message to be encrypted
51
51
* @return The encrypted message as an array of ECPoints (R, S)
52
52
*/
53
- public synchronized ECPoint [] encrypt (String message ) {
53
+ public ECPoint [] encrypt (String message ) {
54
54
BigInteger m = new BigInteger (message .getBytes ()); // Convert message to BigInteger
55
55
SecureRandom r = new SecureRandom (); // Generate random value for k
56
56
BigInteger k = new BigInteger (curve .getFieldSize (), r ); // Generate random scalar k
57
57
58
- // Calculate point R = k * G, where G is the base point
59
- ECPoint R = basePoint .multiply (k , curve .getP (), curve .getA ());
58
+ // Calculate point r = k * G, where G is the base point
59
+ ECPoint rPoint = basePoint .multiply (k , curve .getP (), curve .getA ());
60
60
61
- // Calculate point S = k * publicKey + encodedMessage
62
- ECPoint S = publicKey .multiply (k , curve .getP (), curve .getA ()).add (curve .encodeMessage (m ), curve .getP (), curve .getA ());
61
+ // Calculate point s = k * publicKey + encodedMessage
62
+ ECPoint sPoint = publicKey .multiply (k , curve .getP (), curve .getA ()).add (curve .encodeMessage (m ), curve .getP (), curve .getA ());
63
63
64
- return new ECPoint [] { R , S }; // Return encrypted message as two ECPoints
64
+ return new ECPoint [] {rPoint , sPoint }; // Return encrypted message as two ECPoints
65
65
}
66
66
67
67
/**
@@ -71,12 +71,12 @@ public synchronized ECPoint[] encrypt(String message) {
71
71
* @param encryptedMessage The encrypted message as an array of ECPoints (R, S)
72
72
* @return The decrypted plain message as a String
73
73
*/
74
- public synchronized String decrypt (ECPoint [] encryptedMessage ) {
75
- ECPoint R = encryptedMessage [0 ]; // First part of ciphertext
76
- ECPoint S = encryptedMessage [1 ]; // Second part of ciphertext
74
+ public String decrypt (ECPoint [] encryptedMessage ) {
75
+ ECPoint rPoint = encryptedMessage [0 ]; // First part of ciphertext
76
+ ECPoint sPoint = encryptedMessage [1 ]; // Second part of ciphertext
77
77
78
- // Perform decryption: S - R * privateKey
79
- ECPoint decodedMessage = S .subtract (R .multiply (privateKey , curve .getP (), curve .getA ()), curve .getP (), curve .getA ());
78
+ // Perform decryption: s - r * privateKey
79
+ ECPoint decodedMessage = sPoint .subtract (rPoint .multiply (privateKey , curve .getP (), curve .getA ()), curve .getP (), curve .getA ());
80
80
81
81
BigInteger m = curve .decodeMessage (decodedMessage ); // Decode the message from ECPoint
82
82
@@ -88,7 +88,7 @@ public synchronized String decrypt(ECPoint[] encryptedMessage) {
88
88
*
89
89
* @param bits The size (in bits) of the keys to generate
90
90
*/
91
- public final synchronized void generateKeys (int bits ) {
91
+ public final void generateKeys (int bits ) {
92
92
SecureRandom r = new SecureRandom ();
93
93
curve = new EllipticCurve (bits ); // Initialize a new elliptic curve
94
94
basePoint = curve .getBasePoint (); // Set the base point G
@@ -104,10 +104,10 @@ public final synchronized void generateKeys(int bits) {
104
104
* Class representing an elliptic curve with the form y^2 = x^3 + ax + b.
105
105
*/
106
106
public static class EllipticCurve {
107
- private final BigInteger a ; // Coefficient a in the curve equation
108
- private final BigInteger b ; // Coefficient b in the curve equation
109
- private final BigInteger p ; // Prime number p, defining the finite field
110
- private final ECPoint basePoint ; // Base point G on the curve
107
+ private final BigInteger a ; // Coefficient a in the curve equation
108
+ private final BigInteger b ; // Coefficient b in the curve equation
109
+ private final BigInteger p ; // Prime number p, defining the finite field
110
+ private final ECPoint basePoint ; // Base point G on the curve
111
111
112
112
// Constructor with explicit parameters for a, b, p, and base point
113
113
public EllipticCurve (BigInteger a , BigInteger b , BigInteger p , ECPoint basePoint ) {
@@ -121,8 +121,8 @@ public EllipticCurve(BigInteger a, BigInteger b, BigInteger p, ECPoint basePoint
121
121
public EllipticCurve (int bits ) {
122
122
SecureRandom r = new SecureRandom ();
123
123
this .p = BigInteger .probablePrime (bits , r ); // Random prime p
124
- this .a = new BigInteger (bits , r ); // Random coefficient a
125
- this .b = new BigInteger (bits , r ); // Random coefficient b
124
+ this .a = new BigInteger (bits , r ); // Random coefficient a
125
+ this .b = new BigInteger (bits , r ); // Random coefficient b
126
126
this .basePoint = new ECPoint (BigInteger .valueOf (4 ), BigInteger .valueOf (8 )); // Fixed base point G
127
127
}
128
128
@@ -160,8 +160,8 @@ public BigInteger decodeMessage(ECPoint point) {
160
160
* Class representing a point on the elliptic curve.
161
161
*/
162
162
public static class ECPoint {
163
- private final BigInteger x ; // X-coordinate of the point
164
- private final BigInteger y ; // Y-coordinate of the point
163
+ private final BigInteger x ; // X-coordinate of the point
164
+ private final BigInteger y ; // Y-coordinate of the point
165
165
166
166
public ECPoint (BigInteger x , BigInteger y ) {
167
167
this .x = x ;
@@ -195,12 +195,10 @@ public ECPoint add(ECPoint other, BigInteger p, BigInteger a) {
195
195
BigInteger lambda ;
196
196
if (this .equals (other )) {
197
197
// Special case: point doubling
198
- lambda = (this .x .pow (2 ).multiply (BigInteger .valueOf (3 )).add (a ))
199
- .multiply (this .y .multiply (BigInteger .valueOf (2 )).modInverse (p )).mod (p );
198
+ lambda = (this .x .pow (2 ).multiply (BigInteger .valueOf (3 )).add (a )).multiply (this .y .multiply (BigInteger .valueOf (2 )).modInverse (p )).mod (p );
200
199
} else {
201
200
// General case: adding two different points
202
- lambda = (other .y .subtract (this .y ))
203
- .multiply (other .x .subtract (this .x ).modInverse (p )).mod (p );
201
+ lambda = (other .y .subtract (this .y )).multiply (other .x .subtract (this .x ).modInverse (p )).mod (p );
204
202
}
205
203
206
204
BigInteger xr = lambda .pow (2 ).subtract (this .x ).subtract (other .x ).mod (p );
@@ -226,13 +224,13 @@ public ECPoint multiply(BigInteger k, BigInteger p, BigInteger a) {
226
224
227
225
while (k .signum () > 0 ) {
228
226
if (k .testBit (0 )) {
229
- result = result .add (addend , p , a ); // Add when k's bit is 1
227
+ result = result .add (addend , p , a ); // Add the current point
230
228
}
231
229
addend = addend .add (addend , p , a ); // Double the point
232
- k = k .shiftRight (1 ); // Shift k to the right by 1 bit
230
+ k = k .shiftRight (1 ); // Divide k by 2
233
231
}
234
232
235
- return result ; // Return the resulting point
233
+ return result ;
236
234
}
237
235
}
238
236
}
0 commit comments