如何在C#Windows窗体应用程序中使用aes_encrypt

我一直主要用PHP编写,只知道一种为变量创建aes加密的方法:

aes_encrypt(variableName, 'SecretSalt')

我需要在C#中加密一个变量,我尝试过类似于PHP的方法,但它不起作用(我得到一个错误“当前上下文中不存在名称aes”

这是我目前在C#中的代码:

var username = txtusername.Text;
        var password = txtpassword.Text;
        var usernameAES = aes_encrypt(username, 'mySalt');

使用 AES_ENCRYPT 的正确方法是什么?我将变量发送到PHP Web服务

更新:更多C#代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http;
using System.Net;
using System.IO;
using System.Threading;

        private void Login()
    {
        if (String.IsNullOrEmpty(txtusername.Text))
        {
            MessageBox.Show("Please insert username");
        }

        if (String.IsNullOrEmpty(txtpassword.Text))
        {
            MessageBox.Show("Please insert password");
        }
        var Token = "TMMZC 77385 R8G2D6";
        var username = txtusername.Text;
        var password = txtpassword.Text;
        var usernameAES = aes_encrypt(username, 'mySalt');
        var url = "https://mydomain.co.za/LoginVerification.php?";
        var var = "username=" + username + "&password=" + password + "&Token=" + Token Token;
        var URL = url + var;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.  
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.  
        var responseFromServer = reader.ReadToEnd();
        //MessageBox.Show(responseFromServer);
        // Display the content.  
        if (responseFromServer == "Allow")
        {
            //Open Form1
            Form1 Form = new Form1();
            Form.Show();

            //CLose Password Form
            Password PasswordForm = new Password();
            PasswordForm.Close();
        }

回答(1)

2 years ago

试试这段代码

using System.Security.Cryptography;
using System.IO;


public string EncryptText(string input, string password)
{
    // Get the bytes of the string
    byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(input);
    byte[] passwordBytes = Encoding.UTF8.GetBytes(password);

    // Hash the password with SHA256
    passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

    byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes);

    string result = Convert.ToBase64String(bytesEncrypted);

    return result;
}

public byte[] aes_encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
    byte[] encryptedBytes = null;

    // Set your salt here, change it to meet your flavor:
    // The salt bytes must be at least 8 bytes.
    byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

    using (MemoryStream ms = new MemoryStream())
    {
        using (RijndaelManaged AES = new RijndaelManaged())
        {
            AES.KeySize = 256;
            AES.BlockSize = 128;

            var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
            AES.Key = key.GetBytes(AES.KeySize / 8);
            AES.IV = key.GetBytes(AES.BlockSize / 8);

            AES.Mode = CipherMode.CBC;

            using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
                cs.Close();
            }
            encryptedBytes = ms.ToArray();
        }
    }

    return encryptedBytes;
}