Node.js crypto stops with wrong final block length in final()
Fix it fast
Most common cause, 1 of 4Encrypted bytes were turned into a UTF-8 string
Keep the output as bytes and choose one text encoding for storage.
import crypto from "node:crypto";
function encrypt(text, key, iv) {
const c = crypto.createCipheriv("aes-256-cbc", key, iv);
return Buffer.concat([c.update(text, "utf8"), c.final()]).toString("base64");
}
function decrypt(stored, key, iv) {
const d = crypto.createDecipheriv("aes-256-cbc", key, iv);
return Buffer.concat([d.update(Buffer.from(stored, "base64")), d.final()]).toString("utf8");
}Short answer
error:1C80006B:Provider routines::wrong final block length (ERR_OSSL_WRONG_FINAL_BLOCK_LENGTH) means final() saw a partial block: the AES-CBC ciphertext length is not a multiple of 16 bytes, or is zero. On Node.js 22, 24 and 26 it comes from a lossy string conversion, a cut or altered value, empty input, or encrypting with padding turned off.
Which cause do you have?
Decode the stored ciphertext exactly as your decrypt code does and print its byte length and the remainder after dividing by 16. A remainder other than 0, or a length of 0, tells you the bytes are wrong before any key is involved.
node -e 'const b = Buffer.from(process.argv[1], "base64"); console.log(b.length, b.length % 16)' "<stored ciphertext>"Then find the line you see in this list and open its fix:
- 1Encrypted bytes were turned into a UTF-8 string
The encrypt code joins update() and final() with + or +=, or calls toString() on the result without hex or base64 - 2The stored value was cut short or changed on the way
The length remainder is not 0, the string is shorter than when it was written, or base64 that held + signs now shows spaces - 3decipher.final() ran on empty input
The first step prints 0 0: the field was missing, null or an empty string, so no bytes reached update() - 4Encryption ran with padding switched off
The stack trace points to cipher.final() in the encrypt path, after cipher.setAutoPadding(false)
What does the error look like?
Error: error:1C80006B:Provider routines::wrong final block lengthlibrary: 'Provider routines', reason: 'wrong final block length', code: 'ERR_OSSL_WRONG_FINAL_BLOCK_LENGTH'
Cause 1 of 4
Encrypted bytes were turned into a UTF-8 string
You seeThe encrypt code joins update() and final() with + or +=, or calls toString() on the result without hex or base64
Check
Wrong final block length after a text round trip happens because cipher.update() and cipher.final() return Buffers of raw bytes. Adding them with + converts each one to a UTF-8 string, and byte values that are not valid UTF-8 are replaced, so the stored value no longer has the original length.
grep -rnE "(\+=|\+) *cipher\.final\(\)|final\(\)\.toString\(\)" src/Fix
Keep the output as bytes and choose one text encoding for storage. Concatenate the Buffers, then convert once to hex or base64, and decode with the same name before decrypting.
import crypto from "node:crypto";
function encrypt(text, key, iv) {
const c = crypto.createCipheriv("aes-256-cbc", key, iv);
return Buffer.concat([c.update(text, "utf8"), c.final()]).toString("base64");
}
function decrypt(stored, key, iv) {
const d = crypto.createDecipheriv("aes-256-cbc", key, iv);
return Buffer.concat([d.update(Buffer.from(stored, "base64")), d.final()]).toString("utf8");
}Verify
New records decrypt, and the first-step command prints a length with remainder 0 for each of them. Records written by the old code cannot be repaired, because the replaced bytes are gone.
node -e 'const b = Buffer.from(process.argv[1], "base64"); console.log(b.length % 16 === 0)' "<new ciphertext>"Confirmed by nodejs.org
Cause 2 of 4
The stored value was cut short or changed on the way
You seeThe length remainder is not 0, the string is shorter than when it was written, or base64 that held + signs now shows spaces
Check
A database column that is too narrow or a query string where + is read as a space all change the ciphertext. Node's Buffer docs say base64 decoding ignores spaces, so Buffer.from(..., 'base64') quietly returns fewer bytes, and OpenSSL's block decryption rejects the last partial block with this error.
node -e 'console.log(process.argv[1].length, /\s/.test(process.argv[1]))' "<value as received>"Fix
Widen the column or field to fit the full encoded length, and send ciphertext in URLs and forms as hex, which uses only 0-9 and a-f. Re-read a value straight from storage and compare its length with what was written.
// store and send as hex: no characters that URLs or forms rewrite
const stored = Buffer.concat([c.update(text, "utf8"), c.final()]).toString("hex");
// later
const bytes = Buffer.from(stored, "hex");Verify
The value received equals the value written, character for character, and decipher.final() returns the plaintext.
node -e 'console.log(Buffer.from(process.argv[1], "hex").length % 16)' "<stored hex>"Confirmed by raw.githubusercontent.com
Cause 3 of 4
decipher.final() ran on empty input
You seeThe first step prints 0 0: the field was missing, null or an empty string, so no bytes reached update()
Check
OpenSSL's provider code raises wrong final block length on decryption whenever the buffered data is not exactly one block, unless padding is off and nothing was buffered. An empty ciphertext with default padding therefore throws the same error as a broken one.
node -e 'console.log(JSON.stringify(process.argv[1]))' "<value passed to decrypt>"Fix
Check for a missing or empty value before calling createDecipheriv and treat it as no data, not as ciphertext.
function decryptOrNull(stored, key, iv) {
if (!stored) return null; // nothing was encrypted
return decrypt(stored, key, iv);
}Verify
Rows without an encrypted value return null, and the error appears only if a real value is damaged.
grep -rn "if (!stored)" src/Confirmed by raw.githubusercontent.com
Cause 4 of 4
Encryption ran with padding switched off
You seeThe stack trace points to cipher.final() in the encrypt path, after cipher.setAutoPadding(false)
Check
The same message can come from encryption. Node's docs say that with autoPadding false, the whole input must be a multiple of the cipher's block size or cipher.final() throws, and AES uses 16-byte blocks.
grep -rn "setAutoPadding(false)" src/Fix
Remove setAutoPadding(false) so Node adds standard PKCS padding, unless a partner system demands its own padding scheme. In that case pad the plaintext yourself to a multiple of 16 bytes before update().
const c = crypto.createCipheriv("aes-256-cbc", key, iv);
// c.setAutoPadding(false); // remove unless the other side requires it
const out = Buffer.concat([c.update(text, "utf8"), c.final()]);Verify
The output length is a multiple of 16 for any input length, including inputs that are not.
node -e 'const c=require("crypto");const k=c.randomBytes(32),i=c.randomBytes(16);const x=c.createCipheriv("aes-256-cbc",k,i);console.log(Buffer.concat([x.update("29 bytes of plain text here.."),x.final()]).length)'Confirmed by nodejs.org
How do I stop it coming back?
- Write one encrypt and one decrypt helper that take and return a single named encoding, and ban + between update() and final() in code review.
- Add a unit test that encrypts strings of 0, 1, 15, 16 and 17 characters and decrypts them again, so padding and length bugs fail in CI.
- Size database columns for the encoded ciphertext rather than the plaintext, since both base64 and hex text are longer than the raw bytes they carry.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.