DES Encryption Example
DES Encryption Example
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: