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.

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;

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

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.

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 ?

Hi Gowrishankar,

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

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

yeah, sorry you need using System.Text also

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.