944,111 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 53425
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 5th, 2006
0

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

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Swapnil Palande is offline Offline
13 posts
since May 2006
Jul 5th, 2006
0

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

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

C# Syntax (Toggle Plain Text)
  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
C# Syntax (Toggle Plain Text)
  1. using System.Security.Cryptography;
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Jul 6th, 2006
0

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

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..?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Gowrishankar is offline Offline
13 posts
since Jun 2006
Jul 6th, 2006
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Swapnil Palande is offline Offline
13 posts
since May 2006
Jul 6th, 2006
0

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

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 ?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Gowrishankar is offline Offline
13 posts
since Jun 2006
Jul 6th, 2006
0

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

Hi Gowrishankar,

What is the exact error? And also give the code you have written.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Swapnil Palande is offline Offline
13 posts
since May 2006
Jul 6th, 2006
0

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

Hi swapnil,
Its working now.I gave one namespace System.Text for encoding.
Then Its working properly...Thanks swapnil....
Thanks to campkev....
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Gowrishankar is offline Offline
13 posts
since Jun 2006
Jul 6th, 2006
0

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

yeah, sorry you need using System.Text also
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
May 17th, 2007
0

How to Decrypt a string using C#

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());
}
Reputation Points: 10
Solved Threads: 1
Newbie Poster
satheeshmanian is offline Offline
3 posts
since May 2007
May 17th, 2007
0

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

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.
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: set current time in textbox
Next Thread in C# Forum Timeline: Datagridview to Database table





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC