【问题标题】:How can I add AES for Windows form using SQL Server database如何使用 SQL Server 数据库为 Windows 表单添加 AES
【发布时间】:2014-07-12 05:57:05
【问题描述】:

我需要知道如何加密 SQL Server 数据库中的数据。我使用了各种代码,但在某些程序中 AES 类不存在,我搜索了解决方案,我尝试了很多次才得到它。需要帮助。一切正常,但Aes 出现红色下划线,我没有任何Aes 加密器类。

我使用了这个代码。

private void InsertQuery()
{
     SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

     SqlCommand cmd = new SqlCommand("Insert into data(username,password,age) values (@username,@password,@age)", con);

        try
        {
            if (txtUsername.Text == "")
            {
                MessageBox.Show("Error ! \n Please enter Username");
            }
            else if (txtPassword.Text == "")
            {
                MessageBox.Show("Error ! \n Please enter Password");
            }
            else if (Convert.ToSingle(txtAge.Text) > 99)
            {
                MessageBox.Show("Error ! \n Please enter Age Correctly");
            }
            else
            {
                con.Open();

                cmd.Parameters.AddWithValue("@username", txtUsername.Text.Trim());
                cmd.Parameters.AddWithValue("@password",Encryptdata(txtPassword.Text.Trim()));
                cmd.Parameters.AddWithValue("@age", txtAge.Text);
                cmd.ExecuteNonQuery();
                MessageBox.Show("SUCCESS!");
                con.Dispose();
            }
        }
        catch(Exception e)
        {
            MessageBox.Show("Error! Age is not valid!");
        }
    }

    private string Encryptdata(string clearText)
    {
        string EncryptionKey = "MAKV2SPBNI99212";
        byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);

        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);

            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(clearBytes, 0, clearBytes.Length);
                    cs.Close();
                }
                clearText = Convert.ToBase64String(ms.ToArray());
            }
        }

        return clearText;
    }

    private string Decrypt(string cipherText)
    {
        string EncryptionKey = "MAKV2SPBNI99212";
        byte[] cipherBytes = Convert.FromBase64String(cipherText);

        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);

            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(cipherBytes, 0, cipherBytes.Length);
                    cs.Close();
                }
                cipherText = Encoding.Unicode.GetString(ms.ToArray());
            }
        }

        return cipherText;
    }

    /// <summary>
    /// Function is used to Decrypt the password
    /// </summary>
    /// <param name="password"></param>
    /// <returns></returns>
    private string Decryptdata(string encryptpwd)
    {
        string decryptpwd = string.Empty;
        UTF8Encoding encodepwd = new UTF8Encoding();
        Decoder Decode = encodepwd.GetDecoder();
        byte[] todecode_byte = Convert.FromBase64String(encryptpwd);
        int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
        char[] decoded_char = new char[charCount];
        Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
        decryptpwd = new String(decoded_char);
        return decryptpwd;
    }

【问题讨论】:

    标签: sql encryption aes c#-2.0 sql-server-express


    【解决方案1】:

    请检查This MSDN C#2.0 中不存在 AES,但您有其他 AES 替代方案,例如 Rijndael,您可以通过添加对 System.Security.Cryptography 的引用来使用它,请查看This MSDN

    希望对您有所帮助..!!

    【讨论】:

      【解决方案2】:

      您可以让 SQL Server 处理加密。检查 SQL 函数EncryptByKey

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-10
        相关资源
        最近更新 更多