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

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 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):
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?
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 :)
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...
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’...
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?
Write a Java program to simulate the rolling of two dice. The application should use an...
Write a Java program to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12. Your application should roll the dice 36,000,000 times. Store the results of each roll...
An instructor thinks that math majors can write and debug computer programs faster than the math...
An instructor thinks that math majors can write and debug computer programs faster than the math majors. A sample of 18 business majors took an average of 39 minutes and a sample of 12 math majors took an average of 35 minutes. The standard deviations are 9 and 4 respectively. If the value of the test statistic is 1.44, is there evidence to support the claim? Assume that the variables are normally distributed and the population variances are equal. Use...
In Java, Write a Java program to simulate an ecosystem containing two types of creatures, bears...
In Java, Write a Java program to simulate an ecosystem containing two types of creatures, bears and fish. The ecosystem consists of a river, which is modeled as a relatively large array. Each cell of the array should contain an Animal object, which can be a Bear object, a Fish object, or null. In each time step, based on a random process, each animal either attempts to move into an adjacent array cell or stay where it is. If two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT