65.9K
CodeProject 正在变化。 阅读更多。
Home

C# 电话簿

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.44/5 (24投票s)

2009 年 7 月 17 日

CPOL

1分钟阅读

viewsIcon

171889

downloadIcon

15931

C# 电话簿。

phonebook.png

引言

几天前,我不小心弄丢了我的手机。我所有的联系人号码都在手机里,也丢失了。我奇迹般地找回了我的手机,但决定创建一个电话簿应用程序来存储我的联系人。这个应用程序是用 C# 和 LINQ 编写的。我使用 XML 文件来存储联系人,并使用 3DES 算法对它们进行编码以保护它们。

应用程序功能

实时搜索

您可以实时搜索您的联系人。

多用户

该应用程序可以有多个用户。每个用户只能看到他/她的联系人。

密码提醒

passwordReminder.png

如果您想使用此功能,您需要在 C# 代码中输入有效的 SmptClient 用户名和密码。
此外,您可以输入您的 Gmail 用户名和密码。您需要在 UserForm.cs 中的 忘记密码区域 中输入它们。

try
{
    NetworkCredential loginInfo = new NetworkCredential("username", "password");
    MailMessage msg = new MailMessage();
    msg.From = new MailAddress("sth@gmail.com");
    msg.To.Add(new MailAddress(user.First().Attribute("Email").Value));
    msg.Subject = "Phonebook Password";
    msg.Body = "Yours Password = " + password;
    msg.IsBodyHtml = true;
    SmtpClient client = new SmtpClient("smtp.gmail.com");
    client.EnableSsl = true;
    client.UseDefaultCredentials = false;
    client.Credentials = loginInfo;
    client.Send(msg);

    MessageBox.Show("Your password has been sent to your email", "Email sent",
    	MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

最后,使用新数据编译项目并使用它。

安全

为了保护联系人,我使用了 .NET 3DES 类。

//Base on : http://msdn.microsoft.com/en-us/library/system.security.cryptography.
	//tripledescryptoserviceprovider.aspx
public static void EncryptToFile(String Data, String FileName, byte[] Key, byte[] IV)
{
    try
    {
        // Create or open the specified file.
        FileStream fStream = File.Open(FileName, FileMode.Create);

        // Create a CryptoStream using the FileStream
        // and the passed key and initialization vector (IV).
        CryptoStream cStream = new CryptoStream
				(fStream, new TripleDESCryptoServiceProvider().
        	CreateEncryptor(Key, IV), CryptoStreamMode.Write);

        // Create a StreamWriter using the CryptoStream.
        StreamWriter sWriter = new StreamWriter(cStream);

        // Write the data to the stream
        // to encrypt it.
        sWriter.WriteLine(Data);

        // Close the streams and
        // close the file.
        sWriter.Close();
        cStream.Close();
        fStream.Close();
    }
    catch (CryptographicException e)
    {
        Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
    }
    catch (UnauthorizedAccessException e)
    {
        Console.WriteLine("A file access error occurred: {0}", e.Message);
    }
}

public static string DecryptFromFile(String FileName, byte[] Key, byte[] IV)
{
    try
    {
        // Create or open the specified file.
        FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);

        // Create a CryptoStream using the FileStream
        // and the passed key and initialization vector (IV).
        CryptoStream cStream = new CryptoStream
				(fStream, new TripleDESCryptoServiceProvider().
        	CreateDecryptor(Key, IV), CryptoStreamMode.Read);

        // Create a StreamReader using the CryptoStream.
        StreamReader sReader = new StreamReader(cStream);

        // Read the data from the stream
        // to decrypt it.
        string val = sReader.ReadToEnd();

        // Close the streams and
        // close the file.
        sReader.Close();
        cStream.Close();
        fStream.Close();

        // Return the string.
        return val;
    }
    catch (CryptographicException e)
    {
        Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
        return null;
    }
    catch (UnauthorizedAccessException e)
    {
        Console.WriteLine("A file access error occurred: {0}", e.Message);
        return null;
    }
}

没有人可以查看 XML 文件中的数据,因为它已被编码。我建议在 TripleDES.cs 中更改密钥和向量,并使用您自己的密钥和向量。这些是默认密钥和向量

public static byte[] ByteKey = 
	new byte[] { 65, 20, 35, 105, 249, 97, 242, 87, 163, 127, 124, 121,
	73, 225, 209, 103, 5, 198, 68, 221, 122, 14, 224, 2 };
public static byte[] IV = new byte[] { 160, 175, 98, 111, 208, 167, 177, 23 };

如果您是 3DES 的初学者,可以使用 此应用程序 来更改您的密钥和 IV。

3DESKeyGenerator.png

设置

有一些很棒的设置可以更好地使用该应用程序。

settings.png

您可以更改联系人的方向或日历类型以显示项目的注册日期。您还可以更改应用程序的字体大小。

历史

  • 2009 年 7 月 18 日:首次发布
  • 2009 年 7 月 21 日:更新
  • 2009 年 9 月 9 日:更新
© . All rights reserved.