Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8494901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:25:26+00:00 2026-06-10T23:25:26+00:00

For some odd reason, Node’s built-in Cipher and Decipher classes aren’t working as expected.

  • 0

For some odd reason, Node’s built-in Cipher and Decipher classes aren’t working as expected. The documentation states that cipher.update

“Returns the enciphered contents, and can be called many times with new data as it is streamed.”

The docs also state that cipher.final

“Returns any remaining enciphered contents.”

However, in my tests you must call cipher.final to get all of the data, thus rendering the Cipher object worthless, and to process the next block you have to create a new Cipher object.

var secret = crypto.randomBytes(16)
  , source = crypto.randomBytes(8)
  , cipher = crypto.createCipher("aes128", secret)
  , decipher = crypto.createDecipher("aes128", secret);

var step = cipher.update(source);
var end = decipher.update(step);

assert.strictEqual(source.toString('binary'), end); // should not fail, but does

Note that this happens when using crypto.createCipher or crypto.createCipheriv, with the secret as the initialization vector. The fix is to replace lines 6 and 7 with the following:

var step = cipher.update(source) + cipher.final();
var end = decipher.update(step) + decipher.final();

But this, as previously noted, renders both cipher and decipher worthless.

This is how I expect Node’s built-in cryptography to work, but it clearly doesn’t. Is this a problem with how I’m using it or a bug in Node? Or am I expecting the wrong thing? I could go and implement AES directly, but that would be time-consuming and annoying. Should I just create a new Cipher or Decipher object every time I need to encrypt or decrypt? That seems expensive if I’m doing so as part of a stream.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:25:28+00:00Added an answer on June 10, 2026 at 11:25 pm

    I was having two problems: the first is that I assumed, incorrectly, that the size of a block would be 64 bits, or 8 bytes, which is what I use to create the “plaintext.” In reality the internals of AES split the 128 bit plaintext into two 64 bit chunks, and go from there.

    The second problem was that despite using the correct chunk size after applying the above changes, the crypto module was applying auto padding, and disabling auto padding solved the second problem. Thus, the working example is as follows:

    var secret = crypto.randomBytes(16)
      , source = crypto.randomBytes(16)
      , cipher = crypto.createCipheriv("aes128", secret, secret); // or createCipher
      , decipher = crypto.createDecipheriv("aes128", secret, secret);
    
    cipher.setAutoPadding(false);
    decipher.setAutoPadding(false);
    
    var step = cipher.update(source);
    var end = decipher.update(step);
    
    assert.strictEqual(source.toString('binary'), end); // does not fail
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this asp.NET web site that for some odd reason doesn't want to
i'm getting an error 'Object expected' for some odd reason due to jquery, and
I realized that for some odd reason you can't chain the .Where() after the
So, I have a little code that doesn't work for some odd reason. It's
For some odd reason I recently got an error when working on an Android
For some odd reason if a user tries to send an email to themselves,
For some odd reason the part where objects are shown and hidden in my
For some odd reason, my application likes to break on me when I switch
For some odd reason the HTML5 Audio API won't give me access to currentTime
I've been putting up with this for probably too long. For some odd reason,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.