How to Encrypt a Large File in Java

Written by kevin walker
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Encrypt a Large File in Java
(Hemera Technologies/ Images)

If you need to encrypt small files in Java, it is enough to load all the data into memory, encrypt it and write it back out. However, for larger files that may exceed the size of the computer's memory, this approach will be far too slow. You will instead need to pass data directly in a stream from the file to the newly encrypted file.

Skill level:

Other People Are Reading

Things you need

  • Netbeans

Show MoreHide


  1. 1

    Create a blank text file named "" and paste the following initial code into it:

    import; import; import; import; import; import; import; import; import; import; import; import javax.crypto.Cipher; import javax.crypto.CipherOutputStream; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec;

    public class Encrypter {

    public static void encrypt(SecretKey key, InputStream in, OutputStream out) { }
    public static void main(String[] args) { }


    This has two methods: a main method that will take two arguments--the source file and destination file--and an encrypt method that takes two streams and an encryption key.

  2. 2

    Paste the following within the brackets of the main method:

        File in File = new File(args[0]);
        File outFile = new File(args[1]);
        try {
            SecretKey key = KeyGenerator.getInstance("DES").generateKey();
            Encrypter.encrypt(key, new FileInputStream(inFile), new FileOutputStream(outFile));
        } catch (FileNotFoundException e) {
        } catch (NoSuchAlgorithmException e) {

    This creates a secret key for the Data Encryption Standard (DES) method of encryption and immediately calls the encrypt method, along with the key and a stream for the source and destination files.

  3. 3

    Paste the following into the encrypt method:

         try {
            byte[] iv = new byte[]{
                (byte) 0x8E, 0x12, 0x39, (byte) 0x9C,
                0x07, 0x72, 0x6F, 0x5A
            AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
            Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
            c.init(Cipher.ENCRYPT_MODE, key, paramSpec);
            out = new CipherOutputStream(out, c);
            int count = 0;
            byte[] buffer = new byte[1024];
            while ((count = >= 0) {
                out.write(buffer, 0, count);
        } catch (IOException e) {
        } catch (InvalidAlgorithmParameterException e) {
        } catch (InvalidKeyException e) {
        } catch (NoSuchAlgorithmException e) {
        } catch (NoSuchPaddingException e) {

    This sets up an initialization vector to use for the DES cipher. Data is read from the InputStream, encrypted and written out 1024 bytes at a time, allowing even multi-gigabyte files to be efficiently encrypted without overloading the memory.

  4. 4

    Save your work. If you used a Java Integrated Development Environment (IDE), you can compile by clicking the "Build" button. Otherwise, you will need to run the following command in your command prompt to compile it:


  5. 5

    To encrypt a file, type the following command into a command prompt:

    Java Encrypt source File destination File

    Replace "sourceFile" and "destinationFile" with the filenames needed.

Tips and warnings

  • Using a Java IDE such as Netbeans or Eclipse is recommended. However, if you need to open the command prompt, you can do this in Microsoft Windows by clicking "Start > Run" and typing "cmd" (without quotes).

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.