在C#2.0中使用Registry(Windows窗体)

Kri*_*ota 3 c# registry .net-2.0 winforms

我是Windows Forms的新手.

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

我听说注册表是最好的方式.我对C#.Net中使用注册表一无所知.

所以你能帮助我吗?

如何获取注册表中的值以及如何将值设置为注册表.??

我正在使用.Net Framework 2.0

Ste*_*eve 7

这个主题非常广泛.您应该开始在MSDN上阅读有关该课程的内容

Microsoft.Win32.RegistryKey

但我真的建议完全避免使用注册表.
允许注册表存储正常应用程序的配置信息从一开始就是一个错误.

您可以编写一个简单的散列函数,将其应用于您的用户名和密码,并将结果存储在ApplicationData文件夹中的文件中.在下一次运行时检查文件是否存在,读取它并将其内容与用户名和密码的散列进行比较.

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

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);
}
Run Code Online (Sandbox Code Playgroud)

编辑:根据您的评论,似乎您需要一个加密和解密功能.下面的代码是从Extension Overflow中获取并改编的,您可以在其中找到其他有用的方法.现在,在写入磁盘之前,使用要加密的字符串和密钥调用Encrypt方法.读取后,调用Decrypt方法传递加密文本和密钥.

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;
}
Run Code Online (Sandbox Code Playgroud)

当然,我假设您的应用程序所需的安全级别允许用户名和密码存储在本地系统中.(如您所知,存储在本地系统上的所有内容都不是很安全)