nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.45k stars 278 forks source link

Need help encrypting a message using a key pair #4211

Closed prettydiff closed 12 months ago

prettydiff commented 1 year ago

Details

I can successfully generate key pairs using (TypeScript):

const privateKey:string = "",
  publicKey:string = "",
  callback = function (keyError:NodeJS.ErrnoException, keyPublic:Buffer, keyPrivate:Buffer) {
      if (keyError === null) {
          privateKey = keyPrivate.toString();
          publicKey = keyPublic.toString();

          crypto.privateEncrypt({
              encoding: "utf8",
              format: "pem",
              key: privateKey,
              passphrase: "a real big hash",
              type: "pkcs8"
          }, Buffer.from("some text"));
      } else {
          console.log(keyError);
      }
  },
  options:ED448KeyPairOptions = {
      privateKeyEncoding: {
          cipher: "aes-128-cbc",
          format: "pem",
          passphrase: "a real big hash",
          type: "pkcs8"
      },
      publicKeyEncoding: {
          format: "pem",
          type: "spki"
      }
  };
crypto.generateKeyPair("ed448", options, callback);

The code that errors is the privateEncrypt function. The error:

error:03000096:digital envelope routines::operation not support for this keytype

Here are some combination of things I have tried:

I am not sure why this refuses to work. Could the problem be the use of ed448?

Node.js version

20.5.0

Example code

No response

Operating system

Windows 10

Scope

Runtime

Module and version

Not applicable.

prettydiff commented 1 year ago

Instead trying generateKeyPair with type "ec" and named curve sect571k1 the privateEncrypt function generates error:

error:00000000:lib(0)::reason(0)

https://github.com/prettydiff/share-file-systems/commit/80851d7cf0ba8520f184d1a1325a9febaaf0e72a Shows the actual project code with the commit converting from "ed448" to type "ec".

prettydiff commented 1 year ago

I am going to drop asymmetric key exchange and instead try to solve this problem using a shared secret, probably in the form of HMAC.

tniessen commented 12 months ago

@prettydiff Neither Ed448 nor X448 support encryption. You can use X448 to exchange keys between multiple parties, and you can use Ed448 to digitally sign messages. Neither algorithm is meant for encrypting messages.