Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the past, encryption and decryption were mostly done by substitution and perm

ID: 3850397 • Letter: I

Question

In the past, encryption and decryption were mostly done by substitution and permutation of letters in a text message. In this project, you are required to study those classic cryptographic schemes by reading books in the library or articles on the Internet. Then, develop an automatic cipher using a programming language of your choice. The cipher should be able to

1. generate keys;

2.encrypt a given plaintext message with a key selected from the list of keys generated; and

3.decrypt a given ciphertext message with a known cipher key.

Explanation / Answer

In java 1.8, I implemeted  the following code which will encrypt the testfile and using the Key and decrypt the file using generated key.

1) Generate Key

package com.praveen.code;

import java.io.FileOutputStream;

import java.security.NoSuchAlgorithmException;

import java.util.Scanner;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

public class KeyHandler {

Scanner scan = new Scanner(System.in);

public KeyHandler() {

try {

startMenu();

} catch (Exception e) {

System.out.println("Error during Key Generation:)");

}

}

public void startMenu() throws Exception {

System.out.println(

"Please select the option to generate key" + " " + "1. Generate the AES based Key" + " " + "2. Exit");

int option = Integer.parseInt(scan.nextLine());

do {

switch (option) {

case 1:

generateKey();

break;

case 2:

System.exit(1);

default:

System.out.println("Invalid entry");

}

} while (option != 3);

}

public void generateKey() throws Exception {

try {

KeyGenerator gen = KeyGenerator.getInstance("AES");

gen.init(128);

SecretKey key = gen.generateKey();

byte[] keyBytes = key.getEncoded();

System.out.print("Please enter the filename");

String filename = scan.next();

System.out.println("Generate the key");

FileOutputStream fileOut = new FileOutputStream(filename);

fileOut.write(keyBytes);

fileOut.close();

System.out.println("Generated key for the file " + filename + "...");

System.exit(1);

} catch (NoSuchAlgorithmException e) {

}

}

public static void main(String[] args) {

new KeyHandler();

}

}

2) Encrypt PlainText for text file testfile.txt

package com.praveen.code;

import java.io.ByteArrayOutputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;

import javax.crypto.CipherOutputStream;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;

public class EncryptHandler {

private String encryptedDataString;

private Cipher ecipher;

AlgorithmParameterSpec paramSpec;

byte[] iv;

public EncryptHandler(String dataString, String secretKey, String encryptedDataString) {

this.encryptedDataString = encryptedDataString;

try {

encryptFile(dataString, secretKey);

} catch (Exception e) {

System.out.println("Error during the encryption");

}

}

public void encryptFile(String dataString, String secretKey) throws Exception {

FileInputStream fis = new FileInputStream(dataString);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

int theByte = 0;

while ((theByte = fis.read()) != -1) {

baos.write(theByte);

}

fis.close();

byte[] keyBytes = baos.toByteArray();

baos.close();

SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");

// build the initialization vector. This example is all zeros, but it

// could be any value or generated using a random number generator.

byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

IvParameterSpec ivspec = new IvParameterSpec(iv);

try {

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

ecipher.init(Cipher.ENCRYPT_MODE, keySpec, ivspec);

} catch (Exception e) {

e.printStackTrace();

}

System.out.println("Encrypting file...");

try {

encryptStream(new FileInputStream(dataString), new FileOutputStream(encryptedDataString));

} catch (Exception e) {

e.printStackTrace();

}

}

public void encryptStream(InputStream in, OutputStream out) {

ByteArrayOutputStream bOut = new ByteArrayOutputStream();

byte[] buf = new byte[1024];

try {

out = new CipherOutputStream(out, ecipher);

// read the cleartext and write it to out

int numRead = 0;

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

out.write(buf, 0, numRead);

}

bOut.writeTo(out);

out.close();

bOut.reset();

} catch (java.io.IOException e) {

System.out.println(e);

}

}

public static void main(String[] args) {

String data = "testfile.txt";

String keyFileName = "a";

String encryptedFile = "crypted.txt";

new EncryptHandler(data, keyFileName, encryptedFile);

}

}

3) Decrypt Generation for same file using generated key

package com.praveen.code;

import java.io.ByteArrayOutputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import javax.crypto.Cipher;

import javax.crypto.CipherInputStream;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;

public class DecryptHandler {

public DecryptHandler() {

try {

decryptFile();

} catch (Exception e) {

e.printStackTrace();

System.out.println("Error during the Decrypt");

}

}

public void decryptFile() throws Exception {

byte[] buf = new byte[1024];

String keyFilename = "a";

FileInputStream fis = new FileInputStream(keyFilename);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

int theByte = 0;

while ((theByte = fis.read()) != -1) {

baos.write(theByte);

}

fis.close();

byte[] keyBytes = baos.toByteArray();

baos.close();

SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");

// build the initialization vector. This example is all zeros, but it

// could be any value or generated using a random number generator.

byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

IvParameterSpec ivspec = new IvParameterSpec(iv);

Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");

c.init(Cipher.DECRYPT_MODE, keySpec, ivspec);

System.out.println("Decrypting file...");

try {

decryptStream(new FileInputStream("crypted.txt"), new FileOutputStream("decrypted.txt"), c, buf);

} catch (java.io.IOException e) {

}

System.out.println("File decrypted!");

}

public void decryptStream(InputStream in, OutputStream out, Cipher dcipher, byte[] buf) {

try {

in = new CipherInputStream(in, dcipher);

// Read in the decrypted bytes and write the cleartext to out

int numRead = 0;

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

out.write(buf, 0, numRead);

}

out.close();

} catch (java.io.IOException e) {

System.out.println(e);

}

}

public static void main(String[] args) {

new DecryptHandler();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote