Home » Core Java » security » Java DES Encryption Decryption File Tutorial

About Byron Kiourtzoglou

Avatar photo
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Java DES Encryption Decryption File Tutorial

In this tutorial we are going to see how can you Encrypt and Decrypt a file in Java, using the DES encryption algorithm. DES (Data Encryption Standard) is a block cipher algorithm. It’s one of the most basic symmetric encryption mechanisms, which means that both the encryptor and the decryptor has to know the secret key in order to perform their respective actions.

So the basic steps of this tutorial are :

  • Generate a secure, secret key using a KeyGnerator
  • Create one DES Chiper to encrypt and one to decrypt, using the same secret key as well as specifing an Initialization Vector (IV) for the block algorithm initialization.
  • Write and Read encrypted  or decrypted data  using CipherOutputStream and CipherInputStream

Let’s see the code:

package com.javacodegeeks.java.core;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;

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

public class JavaDESEncryption {

	private static Cipher encrypt;
	private static Cipher decrypt;

	private static final byte[] initialization_vector = { 22, 33, 11, 44, 55, 99, 66, 77 };

	public static void main(String[] args) {

		String clearFile = "C:/Users/nikos7/Desktop/input.txt";
		String encryptedFile = "C:/Users/nikos7/Desktop/encrypted.txt";
		String decryptedFile = "C:/Users/nikos7/Desktop/decrypted.txt";

		try {

			SecretKey secret_key = KeyGenerator.getInstance("DES")
			AlgorithmParameterSpec alogrithm_specs = new IvParameterSpec(

			// set encryption mode ...
			encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
			encrypt.init(Cipher.ENCRYPT_MODE, secret_key, alogrithm_specs);

			// set decryption mode
			decrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
			decrypt.init(Cipher.DECRYPT_MODE, secret_key, alogrithm_specs);

			// encrypt file
			encrypt(new FileInputStream(clearFile), new FileOutputStream(

			// decrypt file
			decrypt(new FileInputStream(encryptedFile), new FileOutputStream(
			System.out.println("End of Encryption/Decryption procedure!");

		} catch (NoSuchAlgorithmException | NoSuchPaddingException
				| InvalidKeyException | InvalidAlgorithmParameterException
				| IOException e) {


	private static void encrypt(InputStream input, OutputStream output)
			throws IOException {

		output = new CipherOutputStream(output, encrypt);
		writeBytes(input, output);

	private static void decrypt(InputStream input, OutputStream output)
			throws IOException {

		input = new CipherInputStream(input, decrypt);
		writeBytes(input, output);

	private static void writeBytes(InputStream input, OutputStream output)
			throws IOException {
		byte[] writeBuffer = new byte[512];
		int readBytes = 0;

		while ((readBytes = input.read(writeBuffer)) >= 0) {
			output.write(writeBuffer, 0, readBytes);




JavaCodeGeeks Rocks!




JavaCodeGeeks Rocks!

This was an example on how to work with DES Encryption/Decryption for a file in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments