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 8731987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:21:24+00:00 2026-06-13T09:21:24+00:00

Does anyone know whether it might be possible to use the javax.crypto.Cipher class to

  • 0

Does anyone know whether it might be possible to use the javax.crypto.Cipher class to progressively fill a sequential chain of fixed size ByteBuffers (pooled buffers) with an arbitrary amount of encrypted data? Without allocating & filling any other intermediary buffer arrays, that is. The encryption for my particular case is AES in CBC mode.

I was hoping for something akin to CharsetEncoder’s encode(CharBuffer input, ByteBuffer output, boolean endOfInput) method, which allows a nice amount of control over writing the output including the ability to write up to a given limit and switch between output buffers as required.

At face value it would appear that this could be achieved using Cipher’s update(ByteBuffer input, ByteBuffer output) method where the limit of the input has been set prior to the call, ie. to avoid overrunning the capacity of the output buffer. If I’m understanding the API docs correctly though, a Cipher implementation may buffer any amount of encrypted data between calls to this method, so conceivably a single call to update() or doFinal() may require an output ByteBuffer with a larger capacity than that available from a fixed size buffer from a pool. In which case, according to the docs for both methods:

If output.remaining() bytes are insufficient to hold the result, a
ShortBufferException is thrown. In this case, repeat this call with a
larger output buffer. Use getOutputSize to determine how big the
output buffer should be.

Does anyone know if there might be a way around this? Perhaps some way of flushing the encrypted data between update() calls to prevent excessive buffer accumulation…? I haven’t yet gone down the path of trying CipherOutputStream. Again, the key goal is to directly populate a series of fixed size ByteBuffers with the encrypted data, without allocating/filling intermediary byte arrays.

  • 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-13T09:21:25+00:00Added an answer on June 13, 2026 at 9:21 am

    For the benefit of anyone who ends up investigating similar needs:

    I did end up using Cipher’s update(ByteBuffer input, ByteBuffer output) method to populate a ByteBuffer, however it was using just the one output buffer rather than switching between a chain of fixed length ones. As the docs suggested (and as much as I tried to avoid this method), calls to getOutputSize() were used to get a ceiling on the output size of the next update() call, and a new buffer was allocated as necessary.

    There’s a limit to how much control you can have over the Cipher output, with or without using CipherOutputStream and its flush() method. It makes sense: if the first 15 bytes of input are submitted to a 128-bit (16 byte) block cipher such as AES, for example, output will always be withheld until further input is received. Flushing may prevent the cipher from buffering more than the block size of data but if you want to use update(ByteBuffer input, ByteBuffer output) to neatly fill as much of the output as it can and then withhold the remaining output for the next call, then you’re out of luck – you would need to provide your own intermediary ‘overflow’ buffer to help dole out the bytes. It’s doable but a bit messy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know whether it is possible to refer to another section from the
Does anyone know whether it's possible to modify the workflow actions of a SharePoint
Does anyone know whether it is possible to change the tab order that tabs
Does anyone know whether if its possible to insert binary data into an SQL
Does anyone know whether it is possible to make a menu in ncurses show
Does anyone know whether it is possible to add columns to a TableLayout in
Does anyone know whether it is possible to programmatically determine the dimensions of the
I have quite an uncommon question: Does anyone know whether it is possible to
Does anyone know whether it's possible to change the header text in a Visual
Does anyone know whether it is possible to create a UDDT that is something

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.