nodejs / help

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

digital envelope routines::unsupported with .mjs #4365

Closed HaoDong108 closed 2 weeks ago

HaoDong108 commented 3 months ago

Details

The first time I used the node .mjs script, I encountered this problem.

(See Example code)

This is just a simple single Demo script. Its file suffix is'. mjs'

On this issue, I refer to stackoverflow,And tried to input set NODE_OPTIONS=--openssl-legacy-provider at the vscode terminal. But it didn't work

I don't want to reduce the version of nodejs, and I don't want to attach other files. Who can tell me how to solve this problem gracefully? 😣

Node.js version

v20.11.1

Example code

import crypto from 'node:crypto';

var obj = {
    code: "GM10001",
    stamp: (new Date()).getTime()
}
var json = JSON.stringify(obj);

var key = Buffer.from("pmQG0EzAXjV1UBCBw4hdDRRKgYnZErCb", "utf8");
var iv = Buffer.from("L434o3PH", "utf8");

//nodejs crypto
var cipher = crypto.createCipheriv('des-cbc', key, iv);   //throw error  0308010C:digital envelope routines::unsupported
var crypted = cipher.update(json, 'utf8', 'hex');
crypted += cipher.final('hex');

console.log(crypted);

Operating system

windows 11
Code edit in vscode

Scope

None

Module and version

Not applicable.

Trott commented 3 months ago

@nodejs/crypto

tniessen commented 2 months ago

@HaoDong108 Why are you using des-cbc?

HaoDong108 commented 2 months ago

@HaoDong108 Why are you using ?des-cbc I want to use it to make a signed Demo, which is used to give the counterpart a demonstration example that can be run directly. But I don't think it has anything to do with what encryption algorithm I use. The question is, it is wrong and strange, or am I using it incorrectly?

HaoDong108 commented 2 months ago

I can't understand why a code that only needs to be implemented purely by algorithm would encounter errors that seem completely unrelated to the calculation process.

RedYetiDev commented 2 weeks ago

But it didn't work

Please provide information about what exactly went wrong

Thanks!

Trott commented 2 weeks ago

But it didn't work

Please provide information about what exactly went wrong

Thanks!

They provide a comment in the code indicating the error they receive on that particular line:

var cipher = crypto.createCipheriv('des-cbc', key, iv);   //throw error  0308010C:digital envelope routines::unsupported
RedYetiDev commented 2 weeks ago

My bad! Sorry for the noise

Trott commented 2 weeks ago

My bad! Sorry for the noise

Actually, I was wrong. They don't say what went wrong when they tried the legacy provider flag. And I see now when I do it myself that the error is different:

RangeError: Invalid key length

Seems like the key is an incompatible length for the chosen algorithm. I'm not a cryptography expert by any means, but changing the key length fixed the error for me.

const crypto = require('crypto')

var obj = {
    code: "GM10001",
    stamp: (new Date()).getTime()
}
var json = JSON.stringify(obj);

var key = "pmQG0EzA";
var iv = Buffer.from("L434o3PH", "utf8");

//nodejs crypto
var cipher = crypto.createCipheriv('DES-CBC', key, iv);   //throw error  0308010C:digital envelope routines::unsupported
var crypted = cipher.update(json, 'utf8', 'hex');
crypted += cipher.final('hex');

console.log(crypted);

I'm going to optimistically close this but not before saying: Please don't use legacy crypto algorithms.