0% found this document useful (0 votes)
25 views3 pages

DES Encryption Example

This document describes a program that implements the Data Encryption Standard (DES) to encrypt and decrypt files. It defines methods to generate encryption/decryption keys, encrypt a source file to a cipher file, and decrypt the cipher file back to a new source file. The main method calls these methods to encrypt a test file, write the encrypted cipher text, and decrypt it back to the original clear text.

Uploaded by

sunilsahani123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

DES Encryption Example

This document describes a program that implements the Data Encryption Standard (DES) to encrypt and decrypt files. It defines methods to generate encryption/decryption keys, encrypt a source file to a cipher file, and decrypt the cipher file back to a new source file. The main method calls these methods to encrypt a test file, write the encrypted cipher text, and decrypt it back to the original clear text.

Uploaded by

sunilsahani123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PROGRAM 5:

DATA ENCRYPTION STANDARD:


import
import
import
import
import
import
import
import
import

java.io.FileInputStream;
java.io.FileOutputStream;
java.io.IOException;
java.io.InputStream;
java.io.OutputStream;
java.security.InvalidAlgorithmParameterException;
java.security.InvalidKeyException;
java.security.NoSuchAlgorithmException;
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 DESEncryptionExample {
private static Cipher encryptCipher;
private static Cipher decryptCipher;
private static final byte[] iv = {11, 22, 33, 44, 99, 88, 77, 66};
public static void main(String[] args)throws IOException {
String clearTextFile = "/home/student/Desktop/DES/source.txt";
String cipherTextFile =
"/home/student/Desktop/DES/cipher.txt";
String clearTextNewFile = "/home/student/Desktop/DES/sourcenew.txt";
//File file = new File("source.txt");
FileInputStream first = new
FileInputStream("/home/student/Desktop/DES/source.txt");
FileInputStream second = new
FileInputStream("/home/student/Desktop/DES/cipher.txt");
FileInputStream third = new
FileInputStream("/home/student/Desktop/DES/source-new.txt");
int oneByte;
System.out.println("Source text is: ");
while ((oneByte = first.read()) != -1) {
System.out.write(oneByte);
}
System.out.flush();
System.out.println("\n");
System.out.println("Cipher text is: ");
while ((oneByte = second.read()) != -1) {
System.out.write(oneByte);
}System.out.println("\n");
System.out.flush();
System.out.println("New Source text is: ");
while ((oneByte = third.read()) != -1) {
System.out.write(oneByte);

}
System.out.flush();
try {
//create SecretKey using KeyGenerator
SecretKey key =
KeyGenerator.getInstance("DES").generateKey();
AlgorithmParameterSpec paramSpec = new
IvParameterSpec(iv);
//get Cipher instance and initiate in encrypt mode
encryptCipher =
Cipher.getInstance("DES/CBC/PKCS5Padding");
encryptCipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
//get Cipher instance and initiate in decrypt mode
decryptCipher =
Cipher.getInstance("DES/CBC/PKCS5Padding");
decryptCipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
//method to encrypt clear text file to encrypted file
encrypt(new FileInputStream(clearTextFile), new
FileOutputStream(cipherTextFile));
//method to decrypt encrypted file to clear text file
decrypt(new FileInputStream(cipherTextFile), new
FileOutputStream(clearTextNewFile));
System.out.println("DONE");
} catch (NoSuchAlgorithmException |
NoSuchPaddingException |
InvalidKeyException |
InvalidAlgorithmParameterException |
IOException e) {
e.printStackTrace();
}
}
private static void encrypt(InputStream is, OutputStream os)
throws IOException {
//create CipherOutputStream to encrypt the data using
encryptCipher
os = new CipherOutputStream(os, encryptCipher);
writeData(is, os);
}
private static void decrypt(InputStream is, OutputStream os)
throws IOException {
//create CipherOutputStream to decrypt the data using
decryptCipher
is = new CipherInputStream(is, decryptCipher);
writeData(is, os);

}
//utility method to read data from input stream and write to
output stream
private static void writeData(InputStream is, OutputStream os)
throws IOException{
byte[] buf = new byte[1024];
int numRead = 0;
//read and write operation
while ((numRead = is.read(buf)) >= 0) {
os.write(buf, 0, numRead);
}
os.close();
is.close();
}
}

OUTPUT:

You might also like