Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cipher.setAutoPadding(false) causes empty ciphertext to be returned #9384

Closed
adelphes opened this issue Oct 31, 2016 · 2 comments
Closed

cipher.setAutoPadding(false) causes empty ciphertext to be returned #9384

adelphes opened this issue Oct 31, 2016 · 2 comments
Labels
crypto Issues and PRs related to the crypto subsystem.

Comments

@adelphes
Copy link

adelphes commented Oct 31, 2016

The following code returns an empty string (with no error) as the output from cipher.final('hex'):

const crypto = require('crypto');

// just use a zero buffer for the key and iv...
const key = Buffer.alloc(32);
const iv = Buffer.alloc(16);

const c = crypto.createCipheriv('aes-256-cbc', key, iv);

// disable auto padding because we are encrypting a complete block length
// - this is what appears to mess up cipher.final()
// - commenting this out or setting setAutoPadding(true) works as expected.
c.setAutoPadding(false);

// encrypt the key (with itself!)
c.update(key);

const encdata = c.final('hex');
// for clarity, stringify the result...
console.log('encdata: ' + JSON.stringify(encdata));

// result should be: encdata: "67695e1f7859caf3cd14f3445b9e5f1b"
// result is actually: encdata: ""

Commenting out the setAutoPadding(false) line returns the correct output.

@Fishrock123 Fishrock123 added the crypto Issues and PRs related to the crypto subsystem. label Oct 31, 2016
@mscdex
Copy link
Contributor

mscdex commented Oct 31, 2016

You're missing the output from .update(). Try this instead:

const encdata = c.update(key, 'buffer', 'hex') + c.final('hex');

@adelphes
Copy link
Author

Yeah, sorry - just realised the problem. Will close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crypto Issues and PRs related to the crypto subsystem.
Projects
None yet
Development

No branches or pull requests

3 participants