In: Computer Science
You have been tasked to write a C# application that will go
through the first step of encrypting an input file and writing the
encrypted contents to an output file.
For this first step in encryption, read each letter in from the
input file, add 3 letters to the value of the input letter. If the
input letter is an A then the encrypted letter would be a D. If the
input letter from the input file is an X then the encrypted letter
would be an A.
All spaces, digits, punctuation, and special characters do not need
to be encrypted at this level. Simply write the space,
punctuation, or special character to the output file as is.
If a letter is uppercase in the input file, it should remain as an
uppercase character in the output file. If a letter is lowercase in
the input file, it should remain as a lowercase character in the
output file.
Since this program runs each month, the input file will be of
various lengths; therefore, the program will read until end of
file.
The input file is found in the current directory and is named
October20.txt. The output
file will be written to the current directory and named
EncryptOct20.txt.
An example:
If the contents of the input file is
Live long and prosper. |
The output file would be:
Olyh orqj dqg survshu. |
What the user should see when the program executes:
When the program begins, the statement "Encryption Level 1" should
be displayed.
The focus should be set to the "Begin Encryption" button.
When the user clicks the "Begin Encryption" button, the program
opens the input file, the statement "Opening input file...." should
be displayed. The "Begin Encryption" button should be
disabled.
When the program begins reading through the input file encrypting
the contents, the statement "Encrypting input file...." should be
displayed.
When the program closes the input and output files, the statement
"Encryption complete...." should be displayed.
When the program is complete, the statement "End of process" should
be displayed.
· Open "Visual Studio 2019"
· Click on "Create a New Project"
· Click on "Windows Forms App (.NET Framework)" -> Click "Next"
· Enter the Project Name as "Encryption" -> Click on "Create"
· Now the screen will look like this:
· Let's change the Form Title to Encryption
· Click on the Form => On the right hand side, Click on "Text" and enter "Encryption " into it
· Go to the Toolbox -> Search for Textbox (Add 4 Textboxes) and Button (Add 1 buttons) to the form
o Align the Textbox and Buttons in the form
o In the Properties box on the right-hand side, Click on the Textbox in the Form and change it's (Name) to txtDisplay. Likewise, change the remaining three Textbox's (Name)'s to , txtOutput1, txtOutput2 and txtOutput3.
o In the Properties box on the right-hand side, Click on the Buttons and change it's (Name) to BtnClickThis and it's Text to Begin Encryption
· For all the Textboxes, select "Read Only" option in the Properties window to True by clicking on the Textbox
· To write the C# code, Right-click on the Form and Click on "View Code" => This will open a Form1.cs file
· The program should do the operation on a Click to the Button. So, Double-Click on the Begin Encryption Button in the Form => A BtnClickThis_Click Function will be added to the Form1.cs file.
· In the Form1.cs, paste the following code,
using System;
using System.IO;
using System.Windows.Forms;
namespace Encrypt
{
public partial class OutputLabel : Form
{
public OutputLabel()
{
InitializeComponent();
}
private void BtnClickThis_Click(object sender, EventArgs e)
{
txtOutput1.Text = "Opening input file....";
BtnClickThis.Enabled = false;
// If you don't want to open a file using Dialog box, Comment the next 10 lines
// and uncomment the line after that
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Text files | *.txt"; // file types, that will be allowed to upload
dialog.Multiselect = false; // allow/deny user to upload more than one file at a time
string text = "";
if (dialog.ShowDialog() == DialogResult.OK) // if user clicked OK
{
String path = dialog.FileName; // get name of file
text = File.ReadAllText(path);
Console.WriteLine(text);
}
//text = File.ReadAllText("October20.txt");
txtOutput1.Text = "Encrypting input file....";
String output = "";
for (int i = 0; i < text.Length; i++)
{
if (System.Convert.ToInt32(text[i]) >= 97 && System.Convert.ToInt32(text[i]) <= 122)
{
if (System.Convert.ToInt32(text[i]) == 120)
{
output += Convert.ToChar(97);
}
else if (System.Convert.ToInt32(text[i]) == 121)
{
output += Convert.ToChar(98);
}
else if (System.Convert.ToInt32(text[i]) == 122)
{
output += Convert.ToChar(99);
}
else
{
output += Convert.ToChar(text[i] + 3);
}
}
else if (System.Convert.ToInt32(text[i]) >= 65 && System.Convert.ToInt32(text[i]) <= 90)
{
if (System.Convert.ToInt32(text[i]) == 88)
{
output += Convert.ToChar(65);
}
else if (System.Convert.ToInt32(text[i]) == 89)
{
output += Convert.ToChar(66);
}
else if (System.Convert.ToInt32(text[i]) == 90)
{
output += Convert.ToChar(67);
}
else
{
output += Convert.ToChar(text[i] + 3);
}
}
else
{
output += text[i];
}
}
string[] WriteText = { output };
txtOutput2.Text = "Encryption complete....";
txtOutput1.Text = "";
string outPath = "E:/EncryptOct20.txt";
File.WriteAllLines(outPath, WriteText);
txtOutput3.Text = "End of process";
txtOutput2.Text = "";
}
private void OutputLabel_Load(object sender, EventArgs e)
{
// Displays this text on starting the application
txtDisplay.Text = "Encryption Level 1";
}
}
}
OUTPUT: