I'm trying to decrypt an encrypted ECDSA_secp256k1 private key generated using the OpenSSL CLI command openssl ecparam -genkey -name secp256k1 | openssl ec -aes-128-cbc -out ecdsa_priv.pem
but I want to do it using pure Javascript. I tried using node-forge, asn1js and pkijs but without success.
Example encrypted key:
Note: I believe the hexValue is the IV (initialization vector) used to generate the key
-----BEGIN EC PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC, `hexValue`
`encrypted key`
-----END EC PRIVATE KEY-----
Basically the steps that I'm trying are:
Extract the decryption algorithm - AES-128-CBC from the example and the IV
Applying the PBKDF2 function - i.e: pbkdf2Sync() function from crypto with the password that I know that the given key is being encrypted with in the first place,
""
empty string as asalt
,1
asiterations
and"sha1"
asdigest
because I've read that these are the default values that OpenSSL uses under the hood. And I want thekeylen
of thederivedKey
to be16
.Then create a custom decipher with
crypto.createDecipheriv('aes-128-cbc', derivedKey, iv);
Then call
await Buffer.concat([decipher.update(pemHex), decipher.final()]));
And get this
error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
Using node-forge
, I got the farthest as I got actually a decrypted private key but it is different than the one that I want to get (I know what should be the decrypted private key in the end). As I dug deeper I saw that generating a derivedKey
with the same arguments with both node-forge
and crypto
was yielding different keys. As I read PBKDF2 should be deterministic, so I assume node-forge
implementation differs somehow and thats why the private key that I'm comparing it to also differs
Can someone please give me some guidance on how to achieve this?