PROGRAM 5:
DATA ENCRYPTION STANDARD:
import
import
import
import
import
import
import
import
import
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
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/[Link]";
String cipherTextFile =
"/home/student/Desktop/DES/[Link]";
String clearTextNewFile = "/home/student/Desktop/DES/[Link]";
//File file = new File("[Link]");
FileInputStream first = new
FileInputStream("/home/student/Desktop/DES/[Link]");
FileInputStream second = new
FileInputStream("/home/student/Desktop/DES/[Link]");
FileInputStream third = new
FileInputStream("/home/student/Desktop/DES/[Link]");
int oneByte;
[Link]("Source text is: ");
while ((oneByte = [Link]()) != -1) {
[Link](oneByte);
}
[Link]();
[Link]("\n");
[Link]("Cipher text is: ");
while ((oneByte = [Link]()) != -1) {
[Link](oneByte);
}[Link]("\n");
[Link]();
[Link]("New Source text is: ");
while ((oneByte = [Link]()) != -1) {
[Link](oneByte);
}
[Link]();
try {
//create SecretKey using KeyGenerator
SecretKey key =
[Link]("DES").generateKey();
AlgorithmParameterSpec paramSpec = new
IvParameterSpec(iv);
//get Cipher instance and initiate in encrypt mode
encryptCipher =
[Link]("DES/CBC/PKCS5Padding");
[Link](Cipher.ENCRYPT_MODE, key, paramSpec);
//get Cipher instance and initiate in decrypt mode
decryptCipher =
[Link]("DES/CBC/PKCS5Padding");
[Link](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));
[Link]("DONE");
} catch (NoSuchAlgorithmException |
NoSuchPaddingException |
InvalidKeyException |
InvalidAlgorithmParameterException |
IOException e) {
[Link]();
}
}
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 = [Link](buf)) >= 0) {
[Link](buf, 0, numRead);
}
[Link]();
[Link]();
}
}
OUTPUT: