Click here to Skip to main content
11,484,752 members (63,662 online)
Click here to Skip to main content

Phone Book in C#

, 9 Sep 2009 CPOL 87.1K 11K 64
Rate this:
Please Sign up or sign in to vote.
Phone book in C#
phonebook.png

Introduction

Some days ago, I lost my cellphone. All of my contact numbers were in the cellphone and I lost them. Miraculously I found my cellphone, but I decided to create a phone book application to store my contacts. This application was written in C# and LINQ. I used an XML file to store contacts and I encoded them with the 3DES algorithm to protect them.

Application Features

Live Search

You could search your contacts as Live.

Multi Users

The application can have several users. Each user can just see his/her contacts.

Password Reminder

passwordReminder.png

If you want to use this feature, you have to enter a valid User name and Password of your SmptClient in C# code.
Also, you can enter your Gmail's user name and password. You have to enter them in Forgets the password region in 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);
}

At last, compile the project with new data and use it.

Security

To protect contacts, I've used .NET 3DES classes.

//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;
    }
}

No one can see your data in the XML file, because it is encoded. I suggest to change the Key and the Vector and use your own ones in TripleDES.cs. These are default Key and Vector:

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 };

If you are a beginner in 3DES, you can use this application to change your Key and IV.

3DESKeyGenerator.png

Settings

There are some cool settings to use the application better.

settings.png

You can change the Direction of contacts or type of Calendar to show item's register date. You can also change the Application's font size.

History

  • 18th July, 2009: First post
  • 21st July, 2009: Updated
  • 9th September, 2009: Updated

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mohammad Dayyan

Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

Comments and Discussions

 
QuestionDate time Pin
Member 956100723-Jan-15 20:29
memberMember 956100723-Jan-15 20:29 
Questionupdate is not updating the data Pin
nrdsp1-May-12 6:12
membernrdsp1-May-12 6:12 
Questionتشکر Pin
amir 212-Jul-11 22:53
memberamir 212-Jul-11 22:53 
Generalmy vote 5 Pin
waqas31611-Nov-10 20:07
memberwaqas31611-Nov-10 20:07 
Generalthnax Dr.Mohammad Dayyan Pin
Ahmad9812-Oct-10 11:57
memberAhmad9812-Oct-10 11:57 
GeneralGood work :) Pin
sunless_201-Jul-10 12:28
membersunless_201-Jul-10 12:28 
GeneralDatabase! Pin
dj_amin12-Mar-10 0:56
memberdj_amin12-Mar-10 0:56 
Ranteyvaal Pin
akbar_pd11-Jan-10 0:46
memberakbar_pd11-Jan-10 0:46 
GeneralMy vote of 1 Pin
Stojaneee3-Jan-10 14:43
memberStojaneee3-Jan-10 14:43 
GeneralRe: My vote of 1 Pin
FernandoUY24-Jan-12 20:48
memberFernandoUY24-Jan-12 20:48 
General[My vote of 2] just ok Pin
Donsw6-Oct-09 16:50
memberDonsw6-Oct-09 16:50 
QuestionWhy Des? Pin
Cuchuk Sergey9-Sep-09 23:27
memberCuchuk Sergey9-Sep-09 23:27 
AnswerRe: Why Des? Pin
Mohammad Dayyan10-Sep-09 0:09
memberMohammad Dayyan10-Sep-09 0:09 
GeneralPrinting report Pin
Blubbo28-Jul-09 2:42
memberBlubbo28-Jul-09 2:42 
GeneralRe: Printing report Pin
Mohammad Dayyan8-Sep-09 11:56
memberMohammad Dayyan8-Sep-09 11:56 
Generalexport and import contact Pin
NiN9E28-Jul-09 2:36
memberNiN9E28-Jul-09 2:36 
GeneralMy vote of 1 Pin
Gwannoes27-Jul-09 21:25
memberGwannoes27-Jul-09 21:25 
GeneralRe: My vote of 1 Pin
FernandoUY24-Jan-12 20:47
memberFernandoUY24-Jan-12 20:47 
GeneralRe: My vote of 1 Pin
Gwannoes6-Feb-12 23:42
memberGwannoes6-Feb-12 23:42 
GeneralRe: My vote of 1 Pin
FernandoUY10-Feb-12 9:16
memberFernandoUY10-Feb-12 9:16 
GeneralMy vote of 1 Pin
Joe Sonderegger27-Jul-09 21:02
memberJoe Sonderegger27-Jul-09 21:02 
GeneralMy vote of 1 Pin
mheidari22-Jul-09 21:50
membermheidari22-Jul-09 21:50 
GeneralRe: My vote of 1 Pin
Mohammad Dayyan23-Jul-09 5:50
memberMohammad Dayyan23-Jul-09 5:50 
General[My vote of 1] This is a junior high school exercise Pin
ITMaiO22-Jul-09 1:35
memberITMaiO22-Jul-09 1:35 
GeneralRe: [My vote of 1] This is a junior high school exercise Pin
Mohammad Dayyan23-Jul-09 5:48
memberMohammad Dayyan23-Jul-09 5:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 9 Sep 2009
Article Copyright 2009 by Mohammad Dayyan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid