MCPcopy Create free account
hub / github.com/TheAlgorithms/Java / encrypt

Method encrypt

src/main/java/com/thealgorithms/ciphers/ECC.java:53–65  ·  view source on GitHub ↗

Encrypts the message using the public key. The message is transformed into an ECPoint and encrypted with elliptic curve operations. @param message The plain message to be encrypted @return The encrypted message as an array of ECPoints (R, S)

(String message)

Source from the content-addressed store, hash-verified

51 * @return The encrypted message as an array of ECPoints (R, S)
52 */
53 public ECPoint[] encrypt(String message) {
54 BigInteger m = new BigInteger(message.getBytes()); // Convert message to BigInteger
55 SecureRandom r = new SecureRandom(); // Generate random value for k
56 BigInteger k = new BigInteger(curve.getFieldSize(), r); // Generate random scalar k
57
58 // Calculate point r = k * G, where G is the base point
59 ECPoint rPoint = basePoint.multiply(k, curve.getP(), curve.getA());
60
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
64 return new ECPoint[] {rPoint, sPoint}; // Return encrypted message as two ECPoints
65 }
66
67 /**
68 * Decrypts the encrypted message using the private key.

Callers

nothing calls this directly

Calls 6

getFieldSizeMethod · 0.80
getPMethod · 0.80
getAMethod · 0.80
encodeMessageMethod · 0.80
multiplyMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected