Hi there guys thanks for you help.
Basically I have created a c# form based rot13 encryption program. The problem I am having is implementing these into a class with methods which can be accessed from the from.
In the main from I have a textbox called message textbox
I have a button called open and decrypt, which uses file dialog to read in a text and decrypt it.
I have a save and encrypt which saved whatever the user enter into the text box and then allow the user to save it to a directory.
I have included the code as you guys will better undertsand this.
Main form code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace FinalAssignment
{
public partial class Form1 : Form
{
public String Message;
public TextBox MessageTexBox;
EncryptionDecryption T = new EncryptionDecryption();
public Form1()
{
InitializeComponent();
}
static class LetterShift
{
public static string Transform(string value)
{
char[] array = value.ToCharArray();
for (int i = 0; i < array.Length; i++)
{
int LetterNumber = (int)array[i];
if (LetterNumber >= 'a' && LetterNumber <= 'z')
{
if (LetterNumber > 'm')
{
LetterNumber -= 13;
}
else
{
LetterNumber += 13;
}
}
else if (LetterNumber >= 'A' && LetterNumber <= 'Z')
{
if (LetterNumber > 'M')
{
LetterNumber -= 13;
}
else
{
LetterNumber += 13;
}
}
array[i] = (char)LetterNumber;
}
return new string(array);
}
}
private void EncryptMessage_Click(object sender, EventArgs e)
{
MessageLabel.Text = "Please enter the message you want encrypted.";
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|";
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
Message = MessageTextBox.Text;
Message = LetterShift.Transform(Message);
StreamWriter sw = new StreamWriter(myStream);
sw.Write(Message);
sw.Close();
MessageTextBox.Text = "";
if
(Message == "")
{
MessageBox.Show("Enter a Message");
}
else
{
MessageBox.Show("Message Encrypted", "Confirmation");
}
myStream.Close();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
MessageTextBox.MaxLength = 300;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
}
private void MessageTextBox_TextChanged(object sender, EventArgs e)
{
}
private void Opefile_Click(object sender, EventArgs e)
{
MessageLabel.Text = "Decrypted Message";
T.OpenFile();
}
private void ClearTextBox_Click(object sender, EventArgs e)
{
MessageTextBox.Text = "";
}
}
}
And the class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace FinalAssignment
{
class EncryptionDecryption
{
public static string john;
public string ss;
static class LetterShift
{
public static string Transform(string value)
{
char[] array = value.ToCharArray();
for (int i = 0; i < array.Length; i++)
{
int LetterNumber = (int)array[i];
if (LetterNumber >= 'a' && LetterNumber <= 'z')
{
if (LetterNumber > 'm')
{
LetterNumber -= 13;
}
else
{
LetterNumber += 13;
}
}
else if (LetterNumber >= 'A' && LetterNumber <= 'Z')
{
if (LetterNumber > 'M')
{
LetterNumber -= 13;
}
else
{
LetterNumber += 13;
}
}
array[i] = (char)LetterNumber;
}
return new string(array);
}
}
public void OpenFile()
{
Stream myStream2;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt";
openFileDialog1.RestoreDirectory = false;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream2 = openFileDialog1.OpenFile()) != null)
{
using (myStream2)
{
StreamReader sr = new StreamReader(myStream2);
ss = sr.ReadToEnd();
ss = LetterShift.Transform(ss);
sr.Close();
MessageBox.Show(ss);
}
}
}
}
}
}
As you can see in the class I have only added the open and decrypt because this is the only thing working. I have set it to show the decrypted message in a message box, however I want this to be displayed in a text file, I cannot access text file properties from the class I wanted some thing like.
MessageTexBot.text = ss; So when the message is read from the file and decrypted it is displayed in the Text Box. I am also trying to put the save button code in a method but when I do it doesn't recognise for exampel this
f
(Message == "")
{
MessageBox.Show("Enter a Message");
}
else
{
MessageBox.Show("Message Encrypted", "Confirmation");
}
This is because message is in another form and this is declared in the class, but even if I make the message public in the other form still the same result.
Also how do I put the rot13 in a method
like so in class
public void encryp()
{
static class LetterShift
{
public static string Transform(string value)
{
char[] array = value.ToCharArray();
for (int i = 0; i < array.Length; i++)
{
int LetterNumber = (int)array[i];
if (LetterNumber >= 'a' && LetterNumber <= 'z')
{
if (LetterNumber > 'm')
{
LetterNumber -= 13;
}
else
{
LetterNumber += 13;
}
}
else if (LetterNumber >= 'A' && LetterNumber <= 'Z')
{
if (LetterNumber > 'M')
{
LetterNumber -= 13;
}
else
{
LetterNumber += 13;
}
}
array[i] = (char)LetterNumber;
}
return new string(array);
}
}
}
I hope someone understands what I am saying, but the actual program works perfectly it is just putting them in method that is the issue.
Thank yooou.