So I am coding a winform app in Visual Studio C# which locks a users type message using a generated key. The user needs to know the date the key was created, which is collected from a file the program generates when the user types into "textBox1". What I am doing is using strings to verify the date the "password.txt" file is created, is the date the user puts in if not the message will not open. The problem though I am having trouble with static properties, this error:
Error CS0120 An object reference is required for the non-static field, method, or property 'Encryptor1.Encrypt(string)' UniveralWindowsTextPGP C:\Users\keife\source\repos\UniveralWindowsTextPGP\UniveralWindowsTextPGP\Form2.cs 85 N/A
Here is where I implement the encryption and decryption method:
namespace UniveralWindowsTextPGP
{
class Encryptor1
{
public static string IV = "1a1a1a1a1a1a1a1a";
public static string Key = "1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a13";
public static string Encrypt(string decrypted)
{
byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(decrypted);
AesCryptoServiceProvider endec = new AesCryptoServiceProvider();
endec.BlockSize = 128;
endec.KeySize = 256;
endec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
endec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
endec.Padding = PaddingMode.PKCS7;
endec.Mode = CipherMode.CBC;
ICryptoTransform icrypt = endec.CreateEncryptor(endec.Key, endec.IV);
byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
icrypt.Dispose();
return Convert.ToBase64String(enc);
}
public static string Decrypted(string encrypted)
{
DateTime creation = File.GetCreationTime(@"C:\password.txt");
FileInfo fi = new FileInfo(@"C:\password.txt");
var created = fi.CreationTime;
if (fi.CreationTime != Form2.keyhere)
// The line above is where the error is occurring in this part.
{
string message = "That is incorrect, access is denied.";
MessageBox.Show(message);
}
else
{
byte[] textbytes = Convert.FromBase64String(encrypted);
AesCryptoServiceProvider endec = new AesCryptoServiceProvider();
endec.BlockSize = 128;
endec.KeySize = 256;
endec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
endec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
endec.Padding = PaddingMode.PKCS7;
endec.Mode = CipherMode.CBC;
ICryptoTransform icrypt = endec.CreateDecryptor(endec.Key, endec.IV);
byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
icrypt.Dispose();
return System.Text.ASCIIEncoding.ASCII.GetString(enc);
}
}
} }
// And here is the form:
namespace UniveralWindowsTextPGP
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void passwordbox_TextChanged(object sender, EventArgs e)
{
}
private void richTextBox1_TextChanged_1(object sender, EventArgs e)
{
}
private void Decrypt_Click(object sender, EventArgs e)
{
string dir = richTextBox1.Text;
StreamReader sr = new StreamReader("encryptedmessagehere.txt");
string line = sr.ReadLine();
richTextBox1.Text = Encryptor1.Decrypted(Convert.ToString(line));
}
private void Form2_Load_1(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
string dir = richTextBox2.Text;
string enctxt = Encryptor1.Encrypt(richTextBox2.Text);
System.IO.File.WriteAllText(@"C:\encryptedmessagehere.txt", enctxt);
}
private void label4_Click(object sender, EventArgs e)
{
}
private void richTextBox2_TextChanged(object sender, EventArgs e)
{
richTextBox2.EnableContextMenu();
}
private void button2_Click(object sender, EventArgs e)
{
string dir = richTextBox2.Text;
StreamReader sr = new StreamReader(@"C:\encryptedmessagehere.txt");
string line = sr.ReadLine();
richTextBox2.Text = Encryptor1.Decrypted(Convert.ToString(line));
System.IO.File.WriteAllText(@"C:\decryptedmessagehere.txt", line);
}
private void button3_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.Show();
}
private void button4_Click(object sender, EventArgs e)
{
Form f4 = new Form4();
f4.Show();
}
public void passwordbox1_TextChanged(object sender, EventArgs e)
{
}
private void richTextBox3_TextChanged(object sender, EventArgs e)
{
string richTextBox3 = "";
}
public void textBox1_TextChanged(object sender, EventArgs e)
{
TextWriter txt = new StreamWriter("C:/password.txt");
txt.Write(textBox1.Text);
txt.Close();
public string keyhere = textBox1.Text;
// This is where the error is occurring here.
Thank you very much, your thoughts are appreciated.
public static string Decrypted(string encrypted, string textFromOtherClass)and while calling itrichTextBox1.Text = Encryptor1.Decrypted(Convert.ToString(line), "passStringHere");@keiferBly