0

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.

3
  • please do not provide your entire code, but only those parts that are relevant for your issue. Most of the code has nothing to do with that. Commented Aug 10, 2020 at 8:13
  • Try to pass as a new parameter instead of accessing directly. public static string Decrypted(string encrypted, string textFromOtherClass) and while calling it richTextBox1.Text = Encryptor1.Decrypted(Convert.ToString(line), "passStringHere"); @keiferBly Commented Aug 10, 2020 at 8:27
  • Running it as public static string Decrypted(string encrypted, string Form2.textBox1) resulted in the text box not being found, is that what you meant? Thanks. Commented Aug 10, 2020 at 8:37

1 Answer 1

1
  1. you cannot declare a class variable in a method.

This line here:

public string keyhere = textBox1.Text;

in textBox1_TextChanged method.

  1. you are using the syntax for the access of a static variable in this line:

    if (fi.CreationTime != Form2.keyhere)

There is no static variable Form2.keyhere. this is why the compiler complains.

You could solve both problems by creating one:

public static string keyhere

public void textBox1_TextChanged(object sender, EventArgs e)
{
    
    TextWriter txt = new StreamWriter("C:/password.txt");
    txt.Write(textBox1.Text);

    keyhere = textBox1.Text;

    txt.Close(); // write this as last call, otherwise textBox1 might already be disposed when you try to access textBox1.Text;
}

Disclaimer: this is an adhoc solution which violates the certain rules for clean coding. Especially the single responsibility principle. Preferably you would pass all necessary information into the Decrypted method and use it there. The class Encryptor1 should not know anything about a Form or other UI elements.

Sign up to request clarification or add additional context in comments.

4 Comments

I just did that thanks! But now on this code if (fi.CreationTime != string keyhere) in the encryption, it returns "invalid expression term "string". I wonder why that might be....
@KeiferBly "But now on this code if (fi.CreationTime != string keyhere)" why would you change the if condition code? I gave you a solution to make your original if condition code working and you changed it and broke it again. The reason why the compiler complains is because you cannot declare a new variable in a comparison expression like a != b the declaration has to happen before that.
Oh sorry was trying to follow someone elses advice at same time. Well I just changed the condition code back and use your solution, it fixed the static issue. But now in if (fi.CreationTime != string Form2.keyhere); It's saying it expects a ) after Form2. I wonder why that might be...
@Keifer Bly You could remove the 'string', and there is no need to declare the type on the right side of ‘!=’. In addition, the return type of ‘fi.CreationTime’ is DateTime, you can try to convert it. As shown below:if ((fi.CreationTime).ToString()!=Form2.keyhere)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.