javascriptnode.jsencryptionaes-gcmscrypt

Why is my decipher.update returning a function and not the deciphered text? NodeJS


I am using the inbuilt crypto module, and been frustrated for many hours trying to figure out why decipher.update returns a function and not the deciphered text itself.

code:

const file = path.join(__dirname, '../secret.txt');
const fileIV = path.join(__dirname, '../iv.txt');
const at = path.join(__dirname, '../at.txt')
var secret = fs.readFileSync(file, 'utf-8');

const algorithm = 'aes-256-gcm';
var text = 'default'
var encrypted = secret;
const iv = crypto.randomBytes(16);

encrypt(plainText, key, iv) {
    const cipher = crypto.createCipheriv(algorithm, key, iv);
    return { encrypted: Buffer.concat([cipher.update(plainText), cipher.final()]), authTag: cipher.getAuthTag() }
}

decrypt(encrypted, key, iv, authTag) {
    const decipher = crypto.createDecipheriv(algorithm, key, iv).setAuthTag(authTag);
    console.log('this worked decrypt');
    return Buffer.concat([decipher.update(encrypted), decipher.final()]);
}

SignUp(pass)
{
    console.log(pass);

    var pair = ec.genKeyPair(); 

    text = pair.getPrivate.toString('hex');

    const key = crypto.scryptSync(pass, 'baethrowssalt', 32);

    console.log(`The key is:${key}`); 

    const {encrypted, authTag} = this.encrypt(text, key, iv);

    console.log('encrypted: ',encrypted.toString('hex'));

    const decrypted = this.decrypt(encrypted, key, iv, authTag);

    console.log('Decrypted:', decrypted.toString('utf-8'));

    return console.log(`Close and reopen your app to integrate your wallet securely`); 
}

in console it prints this when I print out the decrypted result of the private key I initially tried encrypting with scrypt:

Decrypted: function getPrivate(enc) {
  if (enc === 'hex')
    return this.priv.toString(16, 2);
  else
    return this.priv;
}

why is

decrypt(encrypted, key, iv, authTag) {
    const decipher = crypto.createDecipheriv(algorithm, key, iv).setAuthTag(authTag);
    console.log('this worked decrypt');
    return Buffer.concat([decipher.update(encrypted), decipher.final()]);
}

not giving me the text in its deciphered form? Additionally, how can I obtain it since I am clearly doing something wrong. Any help would really be appreciated.


Solution

  • The result of the decryption is exactly the same as the plaintext you encrypted!

    You can easily verify this by outputting the plaintext, i.e. the contents of text, in SignUp() in the console before encrypting it:

    var text = pair.getPrivate.toString('hex');             
    console.log('Initial plaintext:', text);    // Initial plaintext: function getPrivate(enc) {...
    

    The reason for the unexpected content of text is that you simply forgot the pair of parentheses after getPrivate, it should be:

    var text = pair.getPrivate().toString('hex'); 
    console.log('Initial plaintext:', text);    // Initial plaintext: <the hex encoded private key>
    

    Then the decryption provides the expected result.