Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#在我的Winforms应用程序中使用类_C#_Class_Encryption - Fatal编程技术网

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

C#在我的Winforms应用程序中使用类,c#,class,encryption,C#,Class,Encryption,大家好,我正在为学校开发聊天应用程序。它是用C#语言写的,这是我以前从未使用过的语言。现在我有一个winform需要加密一些数据,我有自己的类中的加密代码,但由于某种原因,我不能使用cipher类中的任何函数 下面是代码的一个非常简化的版本 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Syste

大家好,我正在为学校开发聊天应用程序。它是用C#语言写的,这是我以前从未使用过的语言。现在我有一个winform需要加密一些数据,我有自己的类中的加密代码,但由于某种原因,我不能使用cipher类中的任何函数

下面是代码的一个非常简化的版本

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()的函数调用将以红色突出显示,它给出的描述是当前上下文中不存在加密

我对C++有了更多的经验,我发现了一些我所能做的事情,但我想这是不正确的。
非常感谢您的帮助。

SimpleAES
不是一个静态类,因此您需要先创建它的实例,然后才能对其调用方法:

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

在@PoweredByOrange中创建类似answer的simpleas实例,或者在@Bob中更改为static,或者在类似于mine answer的字符串上创建extension方法:

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
    }
}