1

I enountered a problem where I was attempting to derive a 256bit key from an input password in hopes to use it with crypto.createCipheriv/createDecipheriv. This gives me the following error: [ERR_INVALID_ARG_TYPE]: the "key" argument must be of type string or an instance of Buffer, TypedArray, DataView, or KeyObject. Recieved undefined I am not sure why... this.key = crypto.scrypt(this.passphrase, 'baewithsalt',256, (err)=>{if(err) throw err;}); when being used in... this.decipher = crypto.createDecipher(this.algorithm, this.key, this.iv); is telling me the key is undefined. I have these variables initialized just above that... this.passphrase ='whatever'; this.algorithm = 'aes-256-cbc'; this.iv =crypto.randomBytes(16); what am I doing wrong and why can't I use scrypt to generate a key from a password for cipheriv/decipheriv? I apologize if the format may be slightly off, my internet is out and I typed it through my phone.

4N0M41Y
  • 324
  • 1
  • 4
  • 12
  • 3
    [crypto.scrypt is asynchronous](https://nodejs.org/docs/latest-v14.x/api/crypto.html) -- it _returns_ `undefined`, and _later_ when the derived key is ready it calls the callback which takes TWO args: `(err,key)=>{whatever}`. If you want a call that returns the key, use `scryptSync` instead. Also note the keylen argument is in bytes not bits. – dave_thompson_085 Apr 16 '21 at 05:09
  • 1
    A mistake I should have known. I have no idea how I missed that, thank you. – 4N0M41Y Apr 16 '21 at 05:26

0 Answers0