How to generate Curve 25519 key pair with ECDH in node js?

2k views Asked by At

I want to generate a Curve25519 key pair with ECDH for the encryption and decryption. I'm using the Crypto node module to generates a keypair.

But, I failed to generate a keypair with the ECDH algorithm.

NodeJS

const { publicKey, privateKey } = crypto.generateKeyPairSync('x25519', {

publicKeyEncoding: {
   type: 'spki',
   format: 'pem'
},
privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem',
  }
});

Output:

{
    "publicKey": "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VuAyEAR+Bv9DCDFDYih5wHHLp5CALMyUv6AMpuO0lEQkvNMQ8=\n-----END PUBLIC KEY-----\n",
    "privateKey": "-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VuBCIEIMiiTraPY1IO/4m0wV8y4LZgnFy70xR8c8s3nmmxTSFT\n-----END PRIVATE KEY-----\n"
}

Here is one of the Java code using bouncycastle

https://github.com/gsasikumar/forwardsecrecy

Output of Java code:

{
  "privateKey": "-----BEGIN PRIVATE KEY-----MIICRwIBADCB6gYHKoZIzj0CATCB3gIBATArBgcqhkjOPQEBAiB/////////////////////////////////////////7TBEBCAqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqYSRShRAQge0Je0Je0Je0Je0Je0Je0Je0Je0Je0Je0JgtenHcQyGQEQQQqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq0kWiCuGaG4oIa04B7dLHdI0UySPU1+bXxhsinpxaJ+ztPZAiAQAAAAAAAAAAAAAAAAAAAAFN753qL3nNZYEmMaXPXT7QIBCASCAVMwggFPAgEBBCAOeSuzq+O1tPlhPZj4KZdfAq5Z0+yca15ukbUuwsyJQKCB4TCB3gIBATArBgcqhkjOPQEBAiB/////////////////////////////////////////7TBEBCAqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqYSRShRAQge0Je0Je0Je0Je0Je0Je0Je0Je0Je0Je0JgtenHcQyGQEQQQqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq0kWiCuGaG4oIa04B7dLHdI0UySPU1+bXxhsinpxaJ+ztPZAiAQAAAAAAAAAAAAAAAAAAAAFN753qL3nNZYEmMaXPXT7QIBCKFEA0IABG56w6jtAyb2T6oguKNN6HvLPQdT0Mms8gCepW51pobrDT/WfXJKfFr5fRuNalS+YkDQggoc7xtpYFin0ELidYw=-----END PRIVATE KEY-----",
  "errorInfo": null,
  "KeyMaterials": {
    "cryptoAlg": "ECDH",
    "curve": "curve25519",
    "params": "",
    "DHPublicKey": {
      "expiry": "2020-10-15T11:29:20.848Z",
      "Parameter": "",
      "KeyValue": "-----BEGIN PUBLIC KEY-----MIIBMTCB6gYHKoZIzj0CATCB3gIBATArBgcqhkjOPQEBAiB/////////////////////////////////////////7TBEBCAqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqYSRShRAQge0Je0Je0Je0Je0Je0Je0Je0Je0Je0Je0JgtenHcQyGQEQQQqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq0kWiCuGaG4oIa04B7dLHdI0UySPU1+bXxhsinpxaJ+ztPZAiAQAAAAAAAAAAAAAAAAAAAAFN753qL3nNZYEmMaXPXT7QIBCANCAARuesOo7QMm9k+qILijTeh7yz0HU9DJrPIAnqVudaaG6w0/1n1ySnxa+X0bjWpUvmJA0IIKHO8baWBYp9BC4nWM-----END PUBLIC KEY-----"
    }
  }
} 

Can anyone guide me for the same?

0

There are 0 answers