简体   繁体   English

C#在我的Winforms应用程序中使用类

[英]C# Using a Class In My Winforms Application

Hello everyone I am developing a chat application for school. 大家好我正在为学校开发聊天应用程序。 Its in C#, a language I have never worked with before. 它在C#中,我以前从未使用过的语言。 Now I have a winform that needs to encrypt some data, I have the encryption code in is own class but for some reason I can't use any of the functions in the cipher class. 现在我有一个需要加密某些数据的winform,我的加密代码是自己的类,但由于某种原因我不能使用密码类中的任何函数。

Here is a very simplified version of the 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;
using System.Data;
using System.Security.Cryptography;

namespace WindowsFormsApplication2
{


public class SimpleAES
{
    // Change these keys
    private byte[] Key = { 123, 217, 19, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 112, 222, 209, 241, 24, 175, 144, 173, 53, 196, 29, 24, 26, 17, 218, 131, 236, 53, 209 };
    private byte[] Vector = { 146, 64, 191, 111, 23, 3, 113, 119, 231, 121, 252, 112, 79, 32, 114, 156 };


    private ICryptoTransform EncryptorTransform, DecryptorTransform;
    private System.Text.UTF8Encoding UTFEncoder;

    public SimpleAES()
    {
        //This is our encryption method
        RijndaelManaged rm = new RijndaelManaged();

        //Create an encryptor and a decryptor using our encryption method, key, and vector.
        EncryptorTransform = rm.CreateEncryptor(this.Key, this.Vector);
        DecryptorTransform = rm.CreateDecryptor(this.Key, this.Vector);

        //Used to translate bytes to text and vice versa
        UTFEncoder = new System.Text.UTF8Encoding();
    }

    /// -------------- Two Utility Methods (not used but may be useful) -----------
    /// Generates an encryption key.

    public byte[] Encrypt(string TextValue)
    {
        //Translates our text value into a byte array.
        Byte[] bytes = UTFEncoder.GetBytes(TextValue);

        //Used to stream the data in and out of the CryptoStream.
        MemoryStream memoryStream = new MemoryStream();

        /*
         * We will have to write the unencrypted bytes to the stream,
         * then read the encrypted result back from the stream.
         */
        #region Write the decrypted value to the encryption stream
        CryptoStream cs = new CryptoStream(memoryStream, EncryptorTransform, CryptoStreamMode.Write);
        cs.Write(bytes, 0, bytes.Length);
        cs.FlushFinalBlock();
        #endregion

        #region Read encrypted value back out of the stream
        memoryStream.Position = 0;
        byte[] encrypted = new byte[memoryStream.Length];
        memoryStream.Read(encrypted, 0, encrypted.Length);
        #endregion

        //Clean up.
        cs.Close();
        memoryStream.Close();

        return encrypted;
    }



}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        byte[] result = Encrypt(textBox1.Text);


    }
}

}

When I throw this into visual studio the function call to Encrypt() is highlighted in red and the description it gives is the Encrypt does not exist in the current context. 当我把它扔进visual studio时,对Encrypt()的函数调用以红色突出显示,它给出的描述是当前上下文中不存在Encrypt。

I am much more experienced with C++ and I figured something like what I have would work, but I guess thats incorrect. 我对C ++有更多的经验,我觉得我的工作会有所作为,但我猜这不正确。

Any help would be most appreciated. 非常感激任何的帮助。

SimpleAES is not a static class, so you'll need to create an instance of it before you can call methods on it: SimpleAES不是静态类,因此您需要先创建它的实例,然后才能调用它的方法:

private void button1_Click(object sender, EventArgs e)
{
    SimpleAES simpleAes = new SimpleAES();
    byte[] result = simpleAes.Encrypt(textBox1.Text);
}

Make instance of SimpleAES like answer in @PoweredByOrange or change to static like @Bob or make extenstion method on a string like mine answer: 使SimpleAES的实例像@PoweredByOrange中的回答一样,或者像@Bob一样更改为静态,或者像我的回答一样对字符串进行扩展方法:

private void button1_Click(object sender, EventArgs e)
{
    byte[] result = textBox1.Text.Encrypt();
}

public class Extensionmethods
{
    public static byte[] Encrypt(this string TextValue)
    {
        //Your code here
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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