I am trying to implement diffie hellman using libsodium but I am getting a different shared secret key when doing an exchange.
async function handleSharedSecret() {
await _sodium.ready;
const sodium = _sodium;
const secretKey = sodium.crypto_kx_client_session_keys(
sodium.crypto_scalarmult_base(
sodium.from_base64(privateKeyRef.current.value)
),
sodium.from_base64(privateKeyRef.current.value),
sodium.from_base64(publicKeyRef.current.value)
);
setSharedSecretKey(sodium.to_base64(secretKey.sharedRx));
}
The output generated is this
Bob's Public Key: _nGMQavOQuMf7FUyUYfaqvfBcj9hAFJPcc-Bo0JHEEw
Alice's Private Key: vrzVDcdX7PyN1BGo00CzJ_vdvWuOnK_sUeHGQbDAZHQ
Shared Secret Key: 8oVOLsEnxq7XLX6ZXuV3wGgjtyGO7bN8SOvFK1BaB0o
Alice's Public Key: SaKUG5MX0m5XP7Tbf8-LjHzhWdxn9Qn6ndRVBP1YeRI
Bob's Private Key: hzFTBbnif8I37ySoDi5eqtEUechU_dBE7n-oFYNENh0
Shared Secret Key: XnwyWbm2kZFddqx67-QAC1K3Sn7trh5Suk15zl4NmcA
The shared secrets keys don't match. Please help me resolve this.
I got it working. Basically you need to define two separate functions, to generate the same shared secret key. If someone has more expertise in this please do explain why.
This will generate the proper shared secret key for encryption and decryption
As you can see we have the same shared secret key by calling separate functions.