encrypt or decrypt password in c# with asp.net

How to encode a password


private string base64Encode(string sData)
{
try
{
byte[] encData_byte = new byte[sData.Length];

encData_byte = System.Text.Encoding.UTF8.GetBytes(sData);

string encodedData = Convert.ToBase64String(encData_byte);

return encodedData;

}
catch(Exception ex)
{
throw new Exception("Error in base64Encode" + ex.Message);
}
}

HOW TO DECODE IT


public string base64Decode(string sData)

{

System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();

System.Text.Decoder utf8Decode = encoder.GetDecoder();

byte[] todecode_byte = Convert.FromBase64String(sData);

int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

char[] decoded_char = new char[charCount];

utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);

string result = new String(decoded_char);

return result;

}



HOW TO USE:

pass the passwd at the time of registration process to that function and save it in database .at the time of login retrive it and decode it and compare with password entered by user,if satisfy then redirect to another page else will be in correct email or userid or password.


calling function for encode



string pwed = base64encode(val);


calling function for decode

string pwed = base64Decode(val);


Article by Abhay Kumar Raut
DNS MVM and .Net Guru Team Lead at CirrusCloud Systems Bhubaneswar,Odisha Thanks and Regards, Abhay Kumar Raut

Follow Abhay Kumar Raut or read 39 articles authored by Abhay Kumar Raut

Comments

Author: Narendra Reddy vajrala03 Apr 2009 Member Level: Gold   Points : 1

I have a encoded value, using this can i get decode value.
bur am not getting using this. it is showing blank only.

Author: Abhay Kumar Raut25 Apr 2009 Member Level: Gold   Points : 0

Hi Narendra,

It should work.I think you have missed somewhere.

Author: Dharmendra Kumar01 Jun 2009 Member Level: Gold   Points : 0

can you give this code in VB please

Author: guru prasad03 Jun 2009 Member Level: Gold   Points : 1

Hi Friend,

Thanks for your code.

It's nice code for encryption & Decryption. It's working...


Thanks & Regards
Guru Prasad.

Author: Preet04 Nov 2009 Member Level: Gold   Points : 1

Its really a nice code and works well for me.
Could you please suggest what is the difference in :

UTF8 UTF7 etc.

and also what is "Base64String

and where this encoding/decoding would be different for other strings.

Author: anilkumar26 Oct 2010 Member Level: Bronze   Points : 0

yeah its working

Guest Author: Henry Raga Trilleras27 Feb 2012

thanks so much. Easy and effective.

Guest Author: Chandradev01 May 2012

Hi

This is not a complete secure code. Here hacker can easily decrypt the sensitive data. Please encrypt the sensitive data with some salt value. So it will be impossible to decrypt. Plz check this artical
http://chandradev819.wordpress.com/2011/04/11/how-to-encrypt-and-decrypt-password-in-asp-net-using-c/

Author: Abhay Kumar Raut13 May 2012 Member Level: Gold   Points : 0

Chandradev

You are right.I have posted it more than 3.5 years back.It's title should be encode and decode password not encrypt decrypt.

Guest Author: shwetha26 May 2012

I got error in the decrypt method..

Error is:The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters.

Can u pls help me....

Guest Author: kevin shah20 Jun 2012

shwetha :
Put encrypted string value to description function it works...

Author: Abhay Kumar Raut23 Jul 2012 Member Level: Gold   Points : 0

Shwetha,Can you please send me the code ?

Guest Author: Hamid29 May 2013

OK,this worked fine and i recommended to all Google+ it to other person that looking for this snippet code,find it and use it.
Thanks so much



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: