簡體   English   中英

C#在我的Winforms應用程序中使用類

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

大家好我正在為學校開發聊天應用程序。 它在C#中,我以前從未使用過的語言。 現在我有一個需要加密某些數據的winform,我的加密代碼是自己的類,但由於某種原因我不能使用密碼類中的任何函數。

這是代碼的一個非常簡化的版本。

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);


    }
}

}

當我把它扔進visual studio時,對Encrypt()的函數調用以紅色突出顯示,它給出的描述是當前上下文中不存在Encrypt。

我對C ++有更多的經驗,我覺得我的工作會有所作為,但我猜這不正確。

非常感激任何的幫助。

SimpleAES不是靜態類,因此您需要先創建它的實例,然后才能調用它的方法:

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

使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