【问题标题】:Encrypt multiple TextBox with For function C#使用 For 函数 C# 加密多个 TextBox
【发布时间】:2016-09-29 17:35:54
【问题描述】:

我需要从文本框中输入多个值,这必须在我关闭窗口时发生。它是这样工作的。

private void frmUser_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        byte[] encryted = System.Text.Encoding.Unicode.GetBytes(txtIp.Text);
        txtIp.Text = Convert.ToBase64String(encryted);

        byte[] encryted2 = System.Text.Encoding.Unicode.GetBytes(txtUser.Text);
        txtUser.Text = Convert.ToBase64String(encryted2);

        byte[] encryted3 = System.Text.Encoding.Unicode.GetBytes(txtPass.Text);
        txtPass.Text = Convert.ToBase64String(encryted3);

        Configuration conf = new Configuration("C:\\conf.ini");
        conf.IniWriteValue("Info", "Ip", txtIp.Text);
        conf.IniWriteValue("Info", "User", txtUser.Text);
        conf.IniWriteValue("Info", "Password", txtPass.Text);

    }

但是,我怎样才能做一个“For”函数,而不是写很多代码?

编辑 这是加密文件。

【问题讨论】:

  • ToBase64String 和/或 Encoding.GetBytes 未加密数据。任何有权访问谷歌的人都可以想出如何扭转它。这几乎相当于将一个短语从英语翻译成西班牙语,不会说西班牙语的人可能会遇到麻烦,但如果用户可以确定它是西班牙语,那么他们可以在互联网的帮助下在几秒钟内将其翻译。还定义For 函数?你这是什么意思?
  • 不需要循环。您的代码简单易懂。在搜索特定文本框控件的控件列表中使用循环会使其更加复杂。
  • @Fabricio Koch,是的,但是我有很多 TextBox,上面的代码只是一个示例。
  • 只是为了证明我的观点是您上面文件中的数据:Password: 12345, User: admin, Ip: 192.168.10.246。我必须写 0 代码来做到这一点,只需将其插入 base64decode.org
  • 用户:管理员密码:12345

标签: c# winforms for-loop


【解决方案1】:

你可以创建一个字节[]列表

例如:

List<byte[]> List = new List<byte[]>();

然后将你的元素添加到列表中

List.Add(encryted1) ...

然后使用Foreach 循环作为:

Foreach(byte[] b in List)
{
b = System.Text.Encoding.Unicode.GetBytes(txtUser.Text);
}

【讨论】:

    【解决方案2】:

    因此,根据您的评论,您有很多文本框。 像这样抓住它们。

            var myControls = this.Controls.Cast<Control>();
            //Gets all the textbox controls on form, you can filter it more by name if you want
            var myTextboxes = myControls.Where(c => c.GetType() == typeof(TextBox));
    
    
            var dictValues = new Dictionary<string, string>();
    
            foreach(var textbox in myTextboxes)
            {
                //Encrypt textbox value
                var encryptedValue = Encrypt(textbox.Text.ToString());
    
                //You can specify what you want for the Key rather than textbox name also.
                dictValues.Add(textbox.Name, encryptedValue);
            }
    
            var conf = new Configuration("C:\\conf.ini");
            //go through dictionary
            foreach(var item in dictValues)
            {
                //write key and value
                conf.IniWriteValue("Info", item.Key, item.Value);
            }
    

    这是 AES256 的加密函数,可用于加密/解密数据

        //AES256 Encryption, add your own key
       private string Encrypt(string clearText)
        {
            string EncryptionKey = "PUTKEYHERE";
            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;
        }
         //Decryption
        private string Decrypt(string cipherText)
        {
            string EncryptionKey = "PUTKEYHERE";
            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;
        }
    

    【讨论】:

      猜你喜欢
      • 2017-03-30
      • 1970-01-01
      • 2017-07-16
      • 2021-10-26
      • 2012-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-29
      相关资源
      最近更新 更多