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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:56:09+00:00 2026-06-14T04:56:09+00:00

I have an encryption algorithm (AES) that accepts a file converted to array byte

  • 0

I have an encryption algorithm (AES) that accepts a file converted to array byte and encrypt it.
Since I am going to process a very large files, the JVM may go out of memory.
I am planing to read the files in multiple byte arrays, each containing some part of the file. Then I iteratively feed the algorithm. Finally, I merge them to produce an encrypted file.

So my question is: Is there any way to read a file part by part to multiple byte arrays?

I thought I could use the following to read the file to a byte array:

    IOUtils.toByteArray(InputStream input).

And then split the array into multiple bytes using:

    Arrays.copyOfRange()

But I am afraid that the code that reads a file to ByteArray will make the JVM to go out of memory.

  • 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-14T04:56:11+00:00Added an answer on June 14, 2026 at 4:56 am

    Look up cipher streams in Java. You can use them to encrypt/decrypt streams on the fly so you don’t have to store the whole thing in memory. All you have to do is copy the regular FileInputStream for your source file to the CipherOutputStream that’s wrapping your FileOutputStream for the encrypted sink file. IOUtils even conveniently contains a copy(InputStream, OutputStream) method to do this copy for you.

    For example:

    public static void main(String[] args) {
        encryptFile("exampleInput.txt", "exampleOutput.txt");
    }
    
    public static void encryptFile(String source, String sink) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(source);
            CipherOutputStream cos = null;
            try {
                cos = new CipherOutputStream(new FileOutputStream(sink), getEncryptionCipher());
                IOUtils.copy(fis, cos);
            } finally {
                if (cos != null)
                    cos.close();
            }
        } finally {
            if (fis != null)
                fis.close();
        }
    }
    
    private static Cipher getEncryptionCipher() {
        // Create AES cipher with whatever padding and other properties you want
        Cipher cipher = ... ;
        // Create AES secret key
        Key key = ... ;
        cipher.init(Cipher.ENCRYPT_MODE, key);
    }
    

    If you need to know the number of bytes that were copied, you can use IOUtils.copyLarge instead of IOUtils.copy if the file sizes exceed Integer.MAX_VALUE bytes (2 GB).

    To decrypt the file, do the same thing, but use CipherInputStream instead ofCipherOutputStream and initialize your Cipher using Cipher.DECRYPT_MODE.

    Take a look here for more info on cipher streams in Java.

    This will save you space because you won’t need to store byte arrays of your own anymore. The only stored byte[] in this system is the internal byte[] of the Cipher, which will get cleared each time enough input is entered and an encrypted block is returned by Cipher.update, or on Cipher.doFinal when the CipherOutputStream is closed. However, you don’t have to worry about any of this since it’s all internal and everything is managed for you.

    Edit: note that this can result in certain encryption exceptions being ignored, particularly BadPaddingException and IllegalBlockSizeException. This behavior can be found in the CipherOutputStream source code. (Granted, this source is from the OpenJDK, but it probably does the same thing in the Sun JDK.) Also, from the CipherOutputStream javadocs:

    This class adheres strictly to the semantics, especially the failure semantics, of its ancestor classes java.io.OutputStream and java.io.FilterOutputStream. This class has exactly those methods specified in its ancestor classes, and overrides them all. Moreover, this class catches all exceptions that are not thrown by its ancestor classes.

    The bolded line here implies that the cryptographic exceptions are ignored, which they are. This may cause some unexpected behavior while trying to read an encrypted file, especially for block and/or padding encryption algorithms like AES. Make a mental note of this that you will get zero or partial output for the encrypted (or decrypted for CipherInputStream) file.

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

Sidebar

Related Questions

I need a CORE that will perform AES-128 Encryption/Decryption. I have searched online but
I have a char* string that I have encoded using AES encryption. This string
How to perform 256 Bit AES Encryption (Using Rijndael algorithm). Requirement: I have implemented
I have this code for AES encryption, can some one verify that this code
I have made a Java application that uses the PBKDF2WithHmacSHA1 encryption algorithm and I
I have some text that is in a file. I want to encrypt this
I'm testing AES encryption functions from this example . I have found that If
I want to encrypt a text file using AES encryption. However,I am not so
I have made simple encryption/decryption method in php that I'm trying to move to
I have a class of encryption that was developed in Microsoft Visual C++ 6.0,

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.