Write a Java, C# or C/C++ program (the choice is yours) for file/directory proce
ID: 3572077 • Letter: W
Question
Write a Java, C# or C/C++ program (the choice is yours) for file/directory processing
according to the following rules. The program requested for this project must have a text menu
like this:
0 – Exit
1 – Select directory
2 – List directory content (first level)
3 – List directory content (all levels)
4 – Delete file
5 – Display file (hexadecimal view)
6 – Encrypt file (XOR with password)
7 – Decrypt file (XOR with password)
Select option:
The menu is displayed and the user must select an option (a number between 0 and 7). The
action corresponding to the selection is performed, then the menu is displayed again and the
user can choose another option. This cycle is repeated until the user selects 0, which exits the
loop and ends the program.
The options are:
0 – Exit
This options ends the program
1 – Select directory
The user is prompted for a directory [absolute] name. This is the first options that must be
selected by the user. All the options below are working on the directory selected here. After
performing several operations on the selected directory, the user can select another directory
and work with it.
2 – List directory content (first level)
This option displays the content of the selected directory on the screen. All the files and sub-
directories from the first level must be displayed (files and directories should be listed
separately). If no directory was selected an error message must be displayed.
3 – List directory content (all levels)
This option displays the content of the selected directory on the screen. All the files and sub-
directories from the first and subsequent levels must be displayed (files and directories should
be listed separately). If no directory was selected an error message must be displayed.
4 – Delete file
This option prompts the user for a filename and deletes that file from the selected directory. If
no directory was selected an error message must be displayed. If the directory does not contain
the file specified by the user, an error message must be displayed. The filename does not
include any path, it’s just the name of the file.
5 – Display file (hexadecimal view)
This option prompts the user for a filename (from the selected directory) and displays the
content of that file on the screen, in hexadecimal view. If no directory was selected an error
message must be displayed. If the directory does not contain the file specified by the user, an
error message must be displayed. The filename does not include any path, it’s just the name of
the file.
Note 1: The hexadecimal view displays each byte of the file in hexadecimal; it should look
something like this, but simpler:
Note2: The ASCII representation, the Current Offset and the Value Preview are NOT
required; also there is no window, no graphics, no colors, just black and white text display.
Being a text only display, you are required to display ONLY the Data Offset and the
Hexadecimal Representation.
6 – Encrypt file (XOR with password)
This option prompts the user for a password (max 256 bytes long, may contain letters, digits,
other characters) and then prompts the user for a filename and encrypts the content of the
selected file using that password. The encryption method is very simple: just XOR the
password with the file content byte after byte; the password being shorter than the file content,
you must repeat the password as needed.
Example:
passwordpasswordpasswordpasswordpasswordpasswordpass
this is the file content that we need to encrypt now
chiphertext is obtained here by XORing byte to byte
Note1: the user must be prompted for the filename of the encrypted file (containing the
ciphertext) as well, otherwise we would need to overwrite the initial file.
Note2: If no directory was selected an error message must be displayed. If the directory does
not contain either of the files specified by the user, an error message must be displayed. The
filenames do not include any path.
7 – Decrypt file (XOR with password)
This option prompts the user for a password (max 256 bytes long, may contain letters, digits,
other characters) and then prompts the user for a filename and decrypts the content of the
selected file using that password. The decryption method is very simple: just XOR the
password with the file content byte after byte; the password being shorter than the file content,
you must repeat the password as needed.
Example:
passwordpasswordpasswordpasswordpasswordpasswordpass
chiphertext is here …
this is the file content that we had initially
Note1: the user must be prompted for the filename of the decrypted file as well, otherwise we
would need to overwrite the initial file.
Note2: it may seem strange that we are using the same operation (XOR) both for encryption
and for decryption, but this is how XOR (exclusive OR) works.
Note3: If no directory was selected an error message must be displayed. If the directory does
not contain either of the files specified by the user, an error message must be displayed. The
filenames do not include any path.
Testing:
1. You should use this file as the test file for Display, Encryption and Decryption.
2. For encryption, use the following password “Qwertyuiop[123$4$567]”
3. After encrypting this file with the above password, you obtain an encrypted file of the
same length; after decrypting that file with the same password you should obtain an
exact replica of this file, and this must be reflected in the Report document by
appropriate screenshots showing the content of the files involved in these operation
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <sstream>
#include <cstdlib>
std::string load(std::string nameOfFile){ //done
std::ifstream input;
input.open(nameOfFile.c_str());
std::string content((std::istreambuf_iterator<char>(input)), std::istreambuf_iterator<char>());
return content;
}
std::string encrypt(std::string content, std::string password){ //also decrypts! :D
std::string out;
int p = 0;
for (int index = 0; index < content.size(); ++index){
out += content[index] xor password[p] * p % 30;
++p;
if (p >= password.size())
p = 0;
}
return out;
}
void save(std::string nameOfFile, std::string content){ //saves a file with content Content
char response;
std::ifstream in(nameOfFile.c_str());
std::ofstream of;
if (!in){ //prevents a file from being overwritten unless the user says it's okay (in the nested IF statement).
in.close();
of.open(nameOfFile.c_str());
of << content;
} else{
in.close();
std::cout << "A file with that name already exists. Overwrite? (y/n) ";
std::cin >> response;
if(response == 'y'){
of.open(nameOfFile.c_str());
of << content;
} else {
std::cout << "Save cancelled. ";
}
}
of.close();
}
int main(){
int answer = 0;
std::string response, data, nameOfFile, password, encrypted;
char responseC;
do{
switch(answer){
case 0:
{
std::cout << "Available commands: ";
std::cout << "'0'. list commands --- ";
std::cout << "'1'. load a file --- ";
std::cout << "'2'. save a file --- ";
std::cout << "'3'. encrypt a file --- ";
std::cout << "'4'. decrypt a file --- ";
std::cout << "'5'. display contents in memory --- ";
std::cout << "'6'. close the program --- ";
std::cout << "Note: You must load a file before saving, encrypting, or decrypting. ";
break;
}
case 1: //Load a file's content into memory, stored in the 'data' variable.
{
std::cout << "enter name of file (with extension) to load: ";
std::cin >> nameOfFile;
data = load(nameOfFile.c_str());
std::cout << "Display contents? (y/n): ";
std::cin >> responseC;
if (responseC == 'y')
std::cout << data << " ";
break;
}
case 2: //Save a file's content.
{
std::cout << "save file as: ";
std::cin >> response;
save(response, data);
break;
}
case 3: //Encrypt the content of 'data' by XORing it against a password.
{
std::cout << "Encrypt " << nameOfFile << "?(y/n) ";
std::cin >> responseC;
if (responseC == 'y'){
//responseC = ' ';
std::cout << "Enter password to decrypt " << nameOfFile << ':' << " ";
std::cin >> password;
std::cout << "Encrypting... ";
data = encrypt(data, password);
}
break;
}
case 4: //Decrypt the content of 'data' by XORing it against a password. If the password is correct,
{ //the data actually makes sense. If it isn't, the contents will appear to be gibberish.
std::cout << "Enter password for file: ";
std::cin >> password;
std::cout << "Decrypting... ";
data = encrypt(data, password); //uses the same same process as encryption.
std::cout << "Done. ";
break;
}
case 5: //Display the contents of the Data variable.
std::cout << "File contents ----- " << data << " ----- ";
break;
case 6:
{
exit(0);
}
default:
std::cout << "Unrecognized command. Enter '0' to view command list ";
}
std::cout << "Enter command: ";
std::cin >> answer;
}while(answer != 6);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.