Thursday, April 18

Encription and Decription of Password In Asp.net C#

Introduction:-
                     I have explained here how to encrypt and decrypt a string value.
 
Description:-
                     It is became mandatory for all developer to store their password in encrypted because of security reason.And it is the best practice for a developer.If someone wants to store something secretly ,then we should go for it .To store a password in encrypt manner ,it will create  headache  for  hackers to find your password easily.
                                If you will store some string value in encrypted way,then it is quite obvious for an developer to decrypt that string for further use in that application.For an example if some store the encrypted  password in database,But while they want to retrieve their password it won't come the exact one rather encrypted one.Hence we will decrypt that string in that scenario.  


Use the following code to encrypt or decrypt your string.
string Key = "A12B80FKSN";
string Salt = "DotNetOcean";
public string EncryptPassword(string Password)
{
byte[] encryptedPass=null;
try
{
RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();
ICryptoTransform encryptor = rc2CSP.CreateEncryptor(Encoding.UTF8.GetBytes(Key), Encoding.UTF8.GetBytes(Salt));
using (MemoryStream memoEncrypt = new MemoryStream())
{
using (CryptoStream cryptEncrypt = new CryptoStream(memoEncrypt , encryptor, CryptoStreamMode.Write))
{
byte[] EncryptTo = Encoding.Unicode.GetBytes(Password);
 
cryptEncrypt.Write(EncryptTo, 0, EncryptTo.Length);
cryptEncrypt.FlushFinalBlock();
 
encryptedPass = memoEncrypt.ToArray();
 
}
}
}
catch (Exception ex)
{
ErrorLog.PrintError(ex.Message, "EncryptedText", "Template");
}
return Convert.ToBase64String(encryptedPass);
}
 
public string DecryptPassword(string Password)
{
RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();
ICryptoTransform decryptor = rc2CSP.CreateDecryptor(Encoding.UTF8.GetBytes(Key), Encoding.UTF8.GetBytes(Salt));
using (MemoryStream memoDecrypt = new MemoryStream(Convert.FromBase64String(Password)))
{
using (CryptoStream cryptDecrypt = new CryptoStream(memoDecrypt, decryptor, CryptoStreamMode.Read))
{
List<Byte> textbytes = new List<byte>();
int tb;
do
{
tb = cryptDecrypt.ReadByte();
if (tb != -1)
{
textbytes.Add(Convert.ToByte(tb));
}
 
}
while (tb != -1);
 
return Encoding.Unicode.GetString(textbytes.ToArray());
}
}
}
Here I am providing one Alternative Way to secure your text.
//For Encryption use this method
private string Encryptdata(string password)
{
string strmsg = string.Empty;
byte[] encode = new byte[password.Length];
encode = Encoding.UTF8.GetBytes(password);
strmsg = Convert.ToBase64String(encode);
return strmsg;
}
 //For Decryption you can use this method.
private string Decryptdata(string encryptpwd)
{
string decryptpwd = string.Empty;
UTF8Encoding encodepwd = new UTF8Encoding();
Decoder Decode = encodepwd.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(encryptpwd);
int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
decryptpwd = new String(decoded_char);
return decryptpwd;
}
 
In above post I have explained in two different approaches .So you can use any of these approach to store your password or other secured content with encryption.
Keep Coding......
Thanks Shibashish Mohanty
 

No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

.

ShibashishMnty
shibashish mohanty