954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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.

Swapnil Palande
Newbie Poster
13 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

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

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


oh yeah, and don't forget to use

using System.Security.Cryptography;
campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

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..?

Gowrishankar
Newbie Poster
13 posts since Jun 2006
Reputation Points: 10
Solved Threads: 1
 

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.

Swapnil Palande
Newbie Poster
13 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

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 ?

Gowrishankar
Newbie Poster
13 posts since Jun 2006
Reputation Points: 10
Solved Threads: 1
 

Hi Gowrishankar,

What is the exact error? And also give the code you have written.

Swapnil Palande
Newbie Poster
13 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

Hi swapnil,
Its working now.I gave one namespace System.Text for encoding.
Then Its working properly...Thanks swapnil....
Thanks to campkev....

Gowrishankar
Newbie Poster
13 posts since Jun 2006
Reputation Points: 10
Solved Threads: 1
 

yeah, sorry you need using System.Text also

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

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

satheeshmanian
Newbie Poster
3 posts since May 2007
Reputation Points: 10
Solved Threads: 1
 

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.

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

all you have to do is to fetch the user details (using the user name) from BD
then use the same function to hash the user input password
and compare the two

Yanivhl
Newbie Poster
1 post since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You