Ilias Tsagklis

About Ilias Tsagklis

Ilias Tsagklis is a senior software engineer working in the telecom domain. He is an applications developer in a wide variety of applications/services. Ilias is co-founder and Executive Editor at Java Code Geeks.

Encrypt/Decrypt file/stream with DES

This is an example of how to encrypt or decrypt a file or stream with DES. DES in computing refers to the Data Encryption Standard and is supported by Java. To encrypt or decrypt a file or stream with DES one should perform the following steps:

These are demonstrated in the code snippet(s) below:

package com.javacodegeeks.snippets.core;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
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 EncryptDecryptFileStreamWithDES {

	private static Cipher ecipher;
	private static Cipher dcipher;

	// 8-byte initialization vector
	private static byte[] iv = {

  (byte)0xB2, (byte)0x12, (byte)0xD5, (byte)0xB2,

  (byte)0x44, (byte)0x21, (byte)0xC3, (byte)0xC3
    };

	public static void main(String[] args) {

		try {

			SecretKey key = KeyGenerator.getInstance("DES").generateKey();

			AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

			ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

encrypt(new FileInputStream("cleartext.txt"), new FileOutputStream("encrypted.dat"));

decrypt(new FileInputStream("encrypted.dat"), new FileOutputStream("cleartext-reversed.txt"));

		}
		catch (FileNotFoundException e) {
			System.out.println("File Not Found:" + e.getMessage());
			return;
		}
		catch (InvalidAlgorithmParameterException e) {
			System.out.println("Invalid Alogorithm Parameter:" + e.getMessage());
			return;
		}
		catch (NoSuchAlgorithmException e) {
			System.out.println("No Such Algorithm:" + e.getMessage());
			return;
		}
		catch (NoSuchPaddingException e) {
			System.out.println("No Such Padding:" + e.getMessage());
			return;
		}
		catch (InvalidKeyException e) {
			System.out.println("Invalid Key:" + e.getMessage());
			return;
		}

	}

	private static void encrypt(InputStream is, OutputStream os) {

  try {

  	byte[] buf = new byte[1024];

// bytes at this stream are first encoded

  	os = new CipherOutputStream(os, ecipher);

// read in the clear text and write to out to encrypt

int numRead = 0;

while ((numRead = is.read(buf)) >= 0) {

    os.write(buf, 0, numRead);

}

// close all streams

os.close();

  }

  catch (IOException e) {

  	System.out.println("I/O Error:" + e.getMessage());

  }

    }

	private static void decrypt(InputStream is, OutputStream os) {

  try {

  	byte[] buf = new byte[1024];

// bytes read from stream will be decrypted

  	CipherInputStream cis = new CipherInputStream(is, dcipher);

// read in the decrypted bytes and write the clear text to out

int numRead = 0;

while ((numRead = cis.read(buf)) >= 0) {

    os.write(buf, 0, numRead);

}

// close all streams

cis.close();

is.close();

os.close();

  }

  catch (IOException e) {

  	System.out.println("I/O Error:" + e.getMessage());

  }

    }

}

This was an example of how to Encrypt/Decrypt file/stream with DES.

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 two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close