How to Encrypt and Decrypt using C# in window base programme

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2006
Posts: 13
Reputation: Swapnil Palande is an unknown quantity at this point 
Solved Threads: 0
Swapnil Palande Swapnil Palande is offline Offline
Newbie Poster

How to Encrypt and Decrypt using C# in window base programme

 
0
  #1
Jul 5th, 2006
Dear all,

Please help me in solving following query.

I want to use Encryption and Decryption to add password to database and to retrive password from database.

What is the code for this?

Thanks and regards,

Swapnil.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: How to Encrypt and Decrypt using C# in window base programme

 
0
  #2
Jul 5th, 2006
actually you don't want to be able to decrypt it.

what you do is encrypt the password and save that in the database

when the user logs in, encrypt the password the input on the login screen and compare that to the encrypted password in the database

here is a sample for using MD5 hash to encrypt

  1. public string Encrypt(string inp) {
  2. MD5CryptoServiceProvider hasher = new MD5CryptoServiceProvider();
  3. byte[] tBytes = Encoding.ASCII.GetBytes(inp);
  4. byte[] hBytes = hash.ComputeHash(tBytes);
  5.  
  6. StringBuilder sb = new StringBuilder();
  7. for (int c=0;c<hBytes.Length;c++)
  8. sb.AppendFormat("{0:x2}",hBytes[c]);
  9.  
  10. return(sb.ToString());
  11. }

oh yeah, and don't forget to use
  1. using System.Security.Cryptography;
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 13
Reputation: Gowrishankar is an unknown quantity at this point 
Solved Threads: 1
Gowrishankar Gowrishankar is offline Offline
Newbie Poster

Re: How to Encrypt and Decrypt using C# in window base programme

 
0
  #3
Jul 6th, 2006
I tried,It shows some error:It asks namespace for the below:
The type or namespace name 'Encoding','hash',StringBuilder','sb' could not be found (are you missing a using directive or an assembly reference?)

What is the namespace for Encoding,hash,StringBuilder,sb..?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 13
Reputation: Swapnil Palande is an unknown quantity at this point 
Solved Threads: 0
Swapnil Palande Swapnil Palande is offline Offline
Newbie Poster

Re: How to Encrypt and Decrypt using C# in window base programme

 
0
  #4
Jul 6th, 2006
Thanks campkev,

I have checked it and it is working fine.

I want code for decryption also. I want both to encrypt and decrypt the message. Can you send the code for decryption?

Thanks and regards,

Swapnil.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 13
Reputation: Gowrishankar is an unknown quantity at this point 
Solved Threads: 1
Gowrishankar Gowrishankar is offline Offline
Newbie Poster

Re: How to Encrypt and Decrypt using C# in window base programme

 
0
  #5
Jul 6th, 2006
Hi swapnil
I tried the same code.But It asks for the namespaces.I posted the errors in the above reply,kindly go through.what I have to do for that ?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 13
Reputation: Swapnil Palande is an unknown quantity at this point 
Solved Threads: 0
Swapnil Palande Swapnil Palande is offline Offline
Newbie Poster

Re: How to Encrypt and Decrypt using C# in window base programme

 
0
  #6
Jul 6th, 2006
Hi Gowrishankar,

What is the exact error? And also give the code you have written.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 13
Reputation: Gowrishankar is an unknown quantity at this point 
Solved Threads: 1
Gowrishankar Gowrishankar is offline Offline
Newbie Poster

Re: How to Encrypt and Decrypt using C# in window base programme

 
0
  #7
Jul 6th, 2006
Hi swapnil,
Its working now.I gave one namespace System.Text for encoding.
Then Its working properly...Thanks swapnil....
Thanks to campkev....
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: How to Encrypt and Decrypt using C# in window base programme

 
0
  #8
Jul 6th, 2006
yeah, sorry you need using System.Text also
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 3
Reputation: satheeshmanian is an unknown quantity at this point 
Solved Threads: 1
satheeshmanian satheeshmanian is offline Offline
Newbie Poster

How to Decrypt a string using C#

 
0
  #9
May 17th, 2007
Hi CampKev,
Your below code is working properly. Thanks a lot. But i want to Decrypt it again into normal text. Because when the user log-on again, I can't check if the password is correct or not(bcz the password in my sql server table is encrypted). so i want to Decrypt agn to check. Please give me the code to do.I'll b v.v.thankful to u.

public string Encrypt(string inp) {
MD5CryptoServiceProvider hasher = new MD5CryptoServiceProvider();
byte[] tBytes = Encoding.ASCII.GetBytes(inp);
byte[] hBytes = hash.ComputeHash(tBytes);

StringBuilder sb = new StringBuilder();
for (int c=0;c<hBytes.Length;c++)
sb.AppendFormat("{0:x2}",hBytes[c]);

return(sb.ToString());
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: How to Encrypt and Decrypt using C# in window base programme

 
0
  #10
May 17th, 2007
you can't decrypt this. This is a one-way hash. It is the best way to handle passwords. You don't want to be able to decrypt them.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum


Views: 31324 | Replies: 10
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC