e-CryptIt Engine Xojo Plugin

EinhugurEd25519.KeyExchange Method

Exchanges public keys to securely get shared secret.

KeyExchange(
   otherPublicKey as String,
   privateKey as String) as String

Parameters

otherPublicKey
The public key coming from the other person.
privateKey
Your private key.

Returns

String
Shared secret. (Note that shared secret is binary data, so not string with any encoding, you can use EncodeHex to represent it as Hex though if wanting to display it)

Shared secret is 32 character long.

Remarks

Key exchange goes as follows:

1. Person A gives their public key to Person B.
2. Person B gives their public key to Person A.
3. Person A uses KeyExchange method, using public Key from Person B, but their own private key.
4. Person B uses KeyExchange method, using public Key from Person A, but their own private key.

Now Person A and Person B will both have generated the same shared secret without having passed any Private key between them.


var message as String = "Some important message"

// Create key pair. (If not passing in seed then random seed is automatically generated)
var keys as new EinhugurEd25519.KeyPair()

// Sign our message with the key pair (Private and public key)
var signature as String = EinhugurEd25519.Sign(message,keys)

MessageBox("Signature: " + EndOfLine + EncodeHex(signature))

// Verify the signature with public key only. (Since person who verifies would only have the public key)
if EinhugurEd25519.Verify(message, signature, keys.PublicKey) then
    MessageBox("Signature was valid")
else
    MessageBox("Signature was not valid")
end if


// Now we test key exchange. In this case we generate second key set, so we can use to
// exchange two public keys to magically create shared secret on both ends.


var keys2 as new EinhugurEd25519.KeyPair()

// We exchange public keys from point of view of holder of the the first key set.
// (As in private A is known, public key A is known and we get public key sent from B)

var sharedSecret as String = EinhugurEd25519.KeyExchange(keys2.PublicKey, keys.PrivateKey)

// Now we exchange keys in opoisite directions, we are person that holds key set B.
// And we get sent public key A
var sharedSecret2 as String = EinhugurEd25519.KeyExchange(keys.PublicKey, keys2.PrivateKey)

// The magic here is that we have exchanged shared secret without knowing each others private key.
if sharedSecret = sharedSecret2 then
    MessageBox("Shared secret matches")
else
    MessageBox("Shared secret does not match")
end if



This function can throw InvalidArgumentException if the keys are of incorrect size.

Supported Platforms:

  • macOS Intel 64 bit
  • macOS Apple Silicon
  • Windows 32 bit
  • Windows 64 bit
  • Windows ARM 64 bit
  • Linux 32 bit
  • Linux 64 bit
  • Linux ARM 32 bit
  • Linux ARM 64 bit
  • iOS
  • See Also

    EinhugurEd25519 Module