Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > To Avoid Sending Passwords over the Network in Plain Text
 

To Avoid Sending Passwords over the Network in Plain Text

I noticed that passwords were sent over the network in plain text. I did not like this, so I made some changes to the desktop and PPC clients to MD5 hash the passwords before sending them over the network. They probably are not the best changes, so please offer your suggestions.

NB: When I refer to MD5 hash, I mean a 32 character string, not a byte array.

First, generate the MD5 hash of an administrator's account's password. (I used MySQL [yes, MySQL] to do this, SELECT MD5("password") ). Then, launch the client, log in as that person, and change the password to the hash you created.

Then, make the following changes to the desktop client.
1) Add a protected, static HashPassword() function to the DataLayer class.
protected static string HashPassword(string password) {
  System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  byte[] unhashedBytes = System.Text.Encoding.ASCII.GetBytes(password);
  byte[] hashedBytes = md5.ComputeHash(unhashedBytes);
  System.Text.StringBuilder passwordBuilder = new System.Text.StringBuilder(32); //length of text MD5 hash
  foreach (byte hashByte in hashedBytes) {
    passwordBuilder.Append(hashByte.ToString("x2"));
  }
  return passwordBuilder.ToString();
}

2) Change the web service call in DataLayer.GetAuthorizationTicket() to:
m_Ticket = m_WsAuth.GetAuthorizationTicket(CurrentUserInformation.UserName, HashPassword(CurrentUserInformation.UserPassword));

3) In DataLayer.InsertUser() add the following line before the web service call:
newUserInfo.UserPassword = HashPassword(newUserInfo.UserPassword);

4) In DataLayer.UpdateUser() add the following line before the web service call:
updatedUserInfo.UserPassword = HashPassword(updatedUserInfo.UserPassword);

5) In DataLayer.ChangePassword() add the following line before the web service call:
updatedUserInfo.UserPassword = HashPassword(updatedUserInfo.UserPassword);

Compile and run the new client. Log in as the admin with the MD5 hashed password. (It should work.) Then, change all the passwords. Everyone should now have a MD5 hashed password.

To change the PPC client, download and add to your project the MD5.cs file found here:
http://www.flowgroup.fr/tech_md5_us.htm

Then, add the following changes to the PPC client code:
1) Add the following code to Service.Login() above the web service call:
FlowGroup.Crypto.MD5 md5 = FlowGroup.Crypto.MD5CryptoServiceProvider.Create();
byte[] unhashedBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(userPassword);
byte[] hashedBytes = md5.ComputeHash(unhashedBytes);
System.Text.StringBuilder passwordBuilder = new System.Text.StringBuilder(32);
foreach (byte hashByte in hashedBytes) {
  passwordBuilder.Append(hashByte.ToString("x2"));
}

2) Change the web service call to:
string ticket = m_login.GetAuthorizationTicket(userName, passwordBuilder.ToString());

Now, recompile and redeploy and everything should work.
MigrationUser 1  Wednesday, September 03, 2003 8:45 PM
A few corrections:

1) In the PocketPC Client, add the following function to the Service class:
protected static string HashPassword(string password) {
  FlowGroup.Crypto.MD5 md5 = FlowGroup.Crypto.MD5CryptoServiceProvider.Create();
  byte[] unhashedBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(password);
  byte[] hashedBytes = md5.ComputeHash(unhashedBytes);
  System.Text.StringBuilder passwordBuilder = new System.Text.StringBuilder(32);
  foreach (byte hashByte in hashedBytes) {
    passwordBuilder.Append(hashByte.ToString("x2"));
  }
  return passwordBuilder.ToString();
}

2) Change the web service call in Service.GetAuthorizationTicket() to:
string ticket = m_login.GetAuthorizationTicket(m_user.LoginName, HashPassword(m_user.Password));

3) Change the web service call in Service.GetTasksAsync() to:
m_login.BeginGetAuthorizationTicket(m_user.LoginName, HashPassword(m_user.Password), new AsyncCallback(GetAuthorizationTicketComplete), projectID);

4) Change the web service call in Service.Login() to:
string ticket = m_login.GetAuthorizationTicket(userName, HashPassword(userPassword));

This overwrites the changes mentioned in my post above.
MigrationUser 1  Thursday, September 04, 2003 4:03 PM

You can use google to search for other answers

Custom Search

More Threads

• What is the data member when using a stored procedure?
• CImage Capture on Internet Explorer after
• Remote Server Is Unreachable - New Install
• 16 bit Windows Subsystem
• Update of Projects
• How you restart your game...
• TaskVision
• programming code that lets you draw a polygon using visual basic?
• Serialization permissions
• VS 2005 Pro - How do I make my own control show move not resize handles?