Home » Core Java » crypto » Encrypt/Decrypt file/stream with DES

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He 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 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 ....

 

 

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns