在C#与注册工作2.0(Windows窗体) [英] Working with Registry in C# 2.0 (Windows Forms)

查看:156
本文介绍了在C#与注册工作2.0(Windows窗体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手到Windows窗体。

I'm a newbie to Windows Forms.

我在.Net框架2.0设计一个Windows应用程序中,
我需要在系统某处存储用户名和密码并访问他们的每一次我打开我的应用程序和有些时候我需要更改用户命令的凭据。

I'm designing an Windows Application in .Net Framework 2.0 in which, I need to Store a UserName and Password somewhere in the System and access them every time I open My Application and Some times I need to change those credentials on User command.

我听说注册表是这样做的最佳方式。我什么都不知道在C#.NET中使用注册表。

I heard registry is the Best way in doing so. I know Nothing About Using Registry in C# .Net.

所以,你能帮助我在

如何获取值这是在注册表,以及如何设置价值的注册表。 ??

How To Get Values which are In Registry, and how to Set a Value to Registry. ??

我在使用.NET Framework 2.0

I'm Using .Net Framework 2.0

推荐答案

主题是非常广泛的。你应该开始阅读MSDN上关于类

The subject is very broad. You should start reading on MSDN about the class

Microsoft.Win32.RegistryKey

但是我真的建议完全避免注册表。结果
允许注册表来存储配置信息进行正常的应用已经从一开始微软的错误。

But I really suggest to avoid the registry altogether.
Allowing the registry to store configuration info for normal applications has been a mistake from the start by Microsoft.

您可以写一个简单的哈希函数,将它应用到你的用户名和密码,结果存储在位于的ApplicationData文件夹中的文件。
。在下次运行检查,如果该文件存在,理解它,并比较其与用户名和密码的散列的内容。

You could write a simple hashing function, apply it to your username and password and store the result in a file located in the ApplicationData folder. At the next run check if the file exist, read it and compare its content with the hashing of username and password.

下面一个粗略的例子,只是为了让你开始自己的代码。

Here a rough example, just to let you start on your own code.

private void button1_Click(object sender, EventArgs e)
{
    string user = "Steve";
    string pass = "MyPass";

    string hashedUser = GetHashedText(user);
    string hashedPass = GetHashedText(pass);

    string file = Path.Combine(Environment.GetFolderPath 
                       (Environment.SpecialFolder.ApplicationData), 
                       "MyKeys.txt");

    if (File.Exists(file))
    {
        using (StreamReader sr = new StreamReader(file))
        {
            string recordedUser = sr.ReadLine();
            string recordedPass = sr.ReadLine();
            if (recordedUser == user && recordedPass == pass)
                MessageBox.Show("User validated");
            else
                MessageBox.Show("Invalid user");
        }
    }
    else
    {
        using (StreamWriter sw = new StreamWriter(file, false))
        {
            sw.WriteLine(hashedUser);
            sw.WriteLine(hashedPass);
        }

    }
}

private string GetHashedText(string inputData)
{ 
    byte[] tmpSource;
    byte[] tmpData;
    tmpSource = ASCIIEncoding.ASCII.GetBytes(inputData);
    tmpData = new MD5CryptoServiceProvider().ComputeHash(tmpSource);
    return Convert.ToBase64String(tmpData);
}



编辑:基于您的评论,看来你需要一个墓穴和解密功能。下面的代码是采取从扩展溢出,在这里你可以找到其他有用的方法调整。
现在,写入到磁盘之前,调用与字符串加密方法加密和密钥。看完后,调用解密方法,将加密的文本和密钥。

Based on your comment, it seems that you need a crypt and decrypt function. The code below is taken and adapted from the Extension Overflow, where you can find other useful methods. Now, before write to disk, call the Encrypt method with the string to encrypt and a key. After reading, call the Decrypt method passing the crypted text and the secret key.

string cryptedUser = Encrypt(user, "your_secret_key_ABCDEFG");
....

public string Encrypt(string stringToEncrypt, string key)
{
    if (string.IsNullOrEmpty(stringToEncrypt))
        throw new ArgumentException("An empty string value cannot be encrypted.");
    if (string.IsNullOrEmpty(key))
        throw new ArgumentException("Cannot encrypt using an empty key.");

    CspParameters cspp = new CspParameters();
    cspp.KeyContainerName = key;
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspp);
    rsa.PersistKeyInCsp = true;
    byte[] bytes = rsa.Encrypt(UTF8Encoding.UTF8.GetBytes(stringToEncrypt), true);
    return BitConverter.ToString(bytes);
}

string clearText = Decrypt(cryptedText, "your_secret_key_ABCDEFG");
....

public string Decrypt(string stringToDecrypt, string key)
{
    string result = null;
    if (string.IsNullOrEmpty(stringToDecrypt))
        throw new ArgumentException("An empty string value cannot be encrypted.");
    if (string.IsNullOrEmpty(key))
        throw new ArgumentException("Cannot decrypt using an empty key");
    try
    {
        CspParameters cspp = new CspParameters();
        cspp.KeyContainerName = key;
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspp);
        rsa.PersistKeyInCsp = true;
        string[] decryptArray = stringToDecrypt.Split(new string[] { "-" }, 
                                 StringSplitOptions.None);
        byte[] decryptByteArray = Array.ConvertAll<string, byte>
                                 (decryptArray, (s => Convert.ToByte(byte.Parse(s,
                                 System.Globalization.NumberStyles.HexNumber))));
        byte[] bytes = rsa.Decrypt(decryptByteArray, true);
        result = System.Text.UTF8Encoding.UTF8.GetString(bytes);
    }
    finally
    {
        // no need for further processing
    }
    return result;
}



当然,我假设你的应用程序所需的安全级别允许用户名答密码将被存储在本地系统。 (如你所知,这是存储在本地系统上的一切是不是很安全)

Of course, I assume that the security level required by your application allows that username ans passwords will be stored in the local system. (And as you know, everything that is stored on the local system is not very secure)

这篇关于在C#与注册工作2.0(Windows窗体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆