Question

In: Computer Science

Problem 1. Write two computer programs to simulate a Unicode stream cipher that consists of both...

Problem

1. Write two computer programs to simulate a Unicode stream cipher that consists of both
encryption and decryption algorithms. The encryption program accepts inputs from an
existing text file, called “letter.txt.” The encryption program produces an output ciphertext
file, called “secret” The decryption program takes “secret” as input and decrypts it into a
plaintext, called “message.txt.”


- The random “seed” must be known, but be kept secure, by the pseudorandom number
generators in both encryption and decryption programs.

Note from me: Use Java.

Solutions

Expert Solution

PROGRAM :

//EncryptDecrypt.java

import javax.crypto.Cipher;

import javax.crypto.SecretKey;

import javax.crypto.KeyGenerator;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

import java.io.FileWriter;

import java.io.IOException;

public class EncryptDecrypt{

public static void main(String[] args) {

try{

//Generate private key............

KeyGenerator key_generator = KeyGenerator.getInstance("DES");

SecretKey myPrivateKey = key_generator.generateKey();

Cipher cipher;

cipher = Cipher.getInstance("DES");

//Read text from a file named letter.txt

String data = "";

try {

File readFile = new File("/Users/hari-mohan/Desktop/files/letter.txt");

Scanner sc = new Scanner(readFile);

while (sc.hasNextLine()) {

data = sc.nextLine();

}

System.out.println("Read data successfully.");

sc.close();

}

catch (FileNotFoundException e) {

System.out.println("File not found.");

}

//Encrypt the data................

byte[] byteData = data.getBytes("UTF8");

cipher.init(Cipher.ENCRYPT_MODE, myPrivateKey);

byte[] dataEncrypted = cipher.doFinal(byteData);

String s = new String(dataEncrypted);

System.out.println(s);

//Write the encrypted data into the file named secret.txt

try {

FileWriter fileWriter = new FileWriter("/Users/hari-mohan/Desktop/files/secret.txt");

fileWriter.write(s+"");

System.out.println("Write encrypted data successfully.");

fileWriter.close();

} catch (IOException e) {

System.out.println("An error occurred.");

}

//Decrypt the data................

cipher.init(Cipher.DECRYPT_MODE, myPrivateKey);

byte[] textDecrypted = cipher.doFinal(dataEncrypted);

s = new String(textDecrypted);

System.out.println(s);

//Write decrypted data into the file named message.txt

try {

FileWriter fileWriter = new FileWriter("/Users/hari-mohan/Desktop/files/message.txt");

fileWriter.write(s+"");

System.out.println("Write original data successfully.");

fileWriter.close();

} catch (IOException e) {

System.out.println("An error occurred.");

}

}catch(Exception e)

{

System.out.println("Exception"+e.getMessage());

}

}

}


Related Solutions

what are stream manipulators in C++? Write at least 3 short example programs of stream manipulators.
what are stream manipulators in C++? Write at least 3 short example programs of stream manipulators.
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use 4 disks and 3 bars
Problem 1: a. An incoming stream of 1500 lbmol/min consists of saturated water with 62% liquid...
Problem 1: a. An incoming stream of 1500 lbmol/min consists of saturated water with 62% liquid water and 28% steam at 40 bar. This stream is heated at constant pressure to 700ºC. Using the steam tables, what is the heat transfer requirement for this step in kW? b. This steam is then compressed isothermally to steam at 150 bar in a second step. What is the heat transfer requirement for this step in kW?
Problem 2: Caesar Cipher Decryption] Write a python method that takes two parameters: A parameter of...
Problem 2: Caesar Cipher Decryption] Write a python method that takes two parameters: A parameter of type str and a parameter of type int. The first parameter is the plaintext message, and the second parameter is the encryption key. The method strictly does the following tasks: a. Reverse the operations performed by the encryption method to obtain the plaintext message. The method’s header is as follows: def casesardecryption(s, key):
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The program should be able to handle different keys by deciding the key at run time. Thank you :)
Write a program to simulate a calculator. Generate two random numbers (between 1-15) and an ask...
Write a program to simulate a calculator. Generate two random numbers (between 1-15) and an ask the user for an arithmetic operator. Using a switch statement, your program has to perform the arithmetic operation on those two operands depending on what operator the user entered. Ask the user if he/she wants to repeat the calculations and if the answer is yes or YES, keep repeating. If the answer is something else, then stop after the first calculation. c++
Problem 1: Simulating Blackjack In this problem we will use classes and functions to simulate a...
Problem 1: Simulating Blackjack In this problem we will use classes and functions to simulate a simplified game of Blackjack (21). The game begins with a standard deck of 52 playing cards (no jokers). Each player is dealt two cards to start with. The winner of a hand of Blackjack is the player whose hand has the highest value without going over 21. When calculating the value of a hand, we add up the rank of each card in the...
write the outline pseudocode (code is also allowed) for the following problem... Catherine wants to simulate...
write the outline pseudocode (code is also allowed) for the following problem... Catherine wants to simulate a lottery game. What steps would this require? Imagine that Catherine wants a computer to simulate a lottery game. The computer will be generating two-digit numbers. Each time the computer generates a number, the user is first invited to guess what the number is. If the user guesses the exact number, the award is 10 000 EUR. If the user makes a correct guess...
The run times of two computer programs, A and B, are independent random variables. The run...
The run times of two computer programs, A and B, are independent random variables. The run time of program A is an exponential random variable with mean 3 minutes, while that of program B is an Erl(2,lambda) random variable with mean 3 minutes. If both programs are being executed on two identical computers, what is the probability that program B will finish first?
Caesar Cipher Encryption] Write a method that takes two parameters: A parameter of type str and...
Caesar Cipher Encryption] Write a method that takes two parameters: A parameter of type str and a parameter of type int. The first parameter is the plaintext message, and the second parameter is the encryption key. The method strictly does the following tasks: a. Convert the string into a list (let us refer to it as lista). An element in the generated list is the position of the corresponding letter in the parameter string in the English alphabet. Example: ‘C’...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT