I'm trying to implement ECC Algorithm (x25519) in PHP and Javascript.
I'm using sodium in PHP and crypto in javascript, I kind of new to cryptography and It's about my three days searching and trying out libraries. The sodium_crypto_scalarmult function is throwing this error.
Fatal error: Uncaught SodiumException: scalar and point must be SODIUM_CRYPTO_SCALARMULT_SCALARBYTES byte
NodeJS/Javascript Code
const { generateKeyPairSync } = require('crypto');
const { publicKey, privateKey } = generateKeyPairSync('x25519', {
publicKeyEncoding: {
type: 'spki',
format: 'der'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'der'
}
});
console.log("Public Key is: ", publicKey)
console.log("The public key value in base64 is: ", publicKey.toString('base64') );
Because I'm just testing right now before implementing it, I just copy and pasted the base64 public key result from javascript to PHP.
PHP
$publicKeyfromJavascript = "";
$bob = sodium_crypto_box_keypair();
$bobPB = sodium_crypto_box_publickey($bob);
$bobsharedKey = sodium_crypto_scalarmult(sodium_crypto_box_secretkey($bob), $publicKeyfromJavascript);