简体   繁体   中英

How to use a string from another class in c# winform application?

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.

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM