Closed HaoDong108 closed 2 weeks ago
@nodejs/crypto
@HaoDong108 Why are you using des-cbc
?
@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?
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.
But it didn't work
Please provide information about what exactly went wrong
Thanks!
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
My bad! Sorry for the noise
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.
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 workI 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
Operating system
windows 11
Code edit in vscode
Scope
None
Module and version
Not applicable.