.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

How to Encrypt & Decrypt password and stored in database..

Posted By: susant     Posted Date: November 17, 2008    Points:2   Category :C#
Hi All
I need the source code whenever user is entering password in TextBox it will encrypt that password and stored in database and at the time of logon the user will provide his plain passowrd and it will retrive from database match with the user provided password and match.. if it is matching then allow to next page..


susant

Responses
Author: Savi3             
Posted Date: November 17, 2008     Points: 5   

Which type of encryption & decryption u want?...

Author: susant             
Posted Date: November 17, 2008     Points: 5   

Any simple encryption, Decryption technicque.. just encrypt the tpassword value and store in database and at the time of retriving check for user entered correct or not..


susant

Author: Savi3             
Posted Date: November 17, 2008     Points: 5   

I am writng code for 1234 security.

Suppose u assign the text value in variable in string type pwd variable.

Code:
pwd =TextBox1.Text.trim();

string EncryptedPassword=FormsAuthentication.HashPasswordForStoringInConfigFi
le(pwd, "1234");


the variable EncryptedPassword will return Encrpted Password
u can save this value in data base ,

when u r login u can caomapre password like this


string password = FormsAuthentication.HashPasswordForStoringInConfigFile(txtpwd.Text, "1234");

this password value u can check in database in which columns u have stored at the time of creation.

Author: susant             
Posted Date: November 17, 2008     Points: 5   

Hi Siva
I am in windows application.. so without using formauthentication how to do that.. can i implement without using formauthentication.. if yes then send me some code..

Author: Faizal             
Posted Date: November 17, 2008     Points: 5   

HI Try this
This example is based on RJindal Algorithm

using System.Security.Cryptography;
using System.IO;

public static string Encrypt(string clearText, string Password)
{
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});

// PasswordDeriveBytes is for getting Key and IV.
// Using PasswordDeriveBytes object we are first getting 32 bytes for the Key (the default
//Rijndael key length is 256bit = 32bytes) and then 16 bytes for the IV.
// IV should always be the block size, which is by default 16 bytes (128 bit) for Rijndael.

byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return Convert.ToBase64String(encryptedData);
}


Now Encrypt a byte array into a byte array using a key

public static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
{
MemoryStream ms = new MemoryStream();
Rijndael alg = Rijndael.Create();

// Algorithm. Rijndael is available on all platforms.

alg.Key = Key;
alg.IV = IV;
CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);

//CryptoStream is for pumping our data.

cs.Write(clearData, 0, clearData.Length);
cs.Close();
byte[] encryptedData = ms.ToArray();
return encryptedData;
}


Now Decrypt String...

public static string Decrypt(string cipherText, string Password)
{
byte[] cipherBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65,
0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
byte[] decryptedData = Decrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return System.Text.Encoding.Unicode.GetString(decryptedData);
}


Method Extension....

public static byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
{
MemoryStream ms = new MemoryStream();
Rijndael alg = Rijndael.Create();
alg.Key = Key;
alg.IV = IV;
CryptoStream cs = new CryptoStream(ms,alg.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(cipherData, 0, cipherData.Length);
cs.Close();
byte[] decryptedData = ms.ToArray();
return decryptedData;
}


Author: Basawareddy             
Posted Date: December 02, 2009     Points: 5   

Dear Faizal,

I wanted to get 128 bits encrypted format. I am not able to get wat ur passing to Encrypt method.
Please see u r bellow code.

public static string Encrypt(string clearText, string Password)
{
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});

// PasswordDeriveBytes is for getting Key and IV.
// Using PasswordDeriveBytes object we are first getting 32 bytes for the Key (the default
//Rijndael key length is 256bit = 32bytes) and then 16 bytes for the IV.
// IV should always be the block size, which is by default 16 bytes (128 bit) for Rijndael.

byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return Convert.ToBase64String(encryptedData);
}


Wat needs to be passed in this parameter "clearText".

Actually i wanted to encrypt in 128 bit format plz help me.

Regards,
Basawareddy M Gopsen

Author: Faizal             
Posted Date: December 02, 2009     Points: 5   

Hi Basawareddy,

You need to pass the text you want to encrypt in clearText..

Thanks
Faizal

Author: Basawareddy             
Posted Date: December 02, 2009     Points: 5   

Dear Faizal,

Thanks for u r reply.

Please see following method which has 2 parrameters one is clearText and other one is Password.

For example i want to encrypt "Base" word. So i will pass Base as clearText parameter then what needs to be for Password parameter?

public static string Encrypt(string clearText, string Password)
{
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});

// PasswordDeriveBytes is for getting Key and IV.
// Using PasswordDeriveBytes object we are first getting 32 bytes for the Key (the default
//Rijndael key length is 256bit = 32bytes) and then 16 bytes for the IV.
// IV should always be the block size, which is by default 16 bytes (128 bit) for Rijndael.

byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return Convert.ToBase64String(encryptedData);
}


Please help me to solve this issue. And will it encrypt in 128 bit format ?

Regards,
Basawareddy M Gopsen

Author: Meia             
Posted Date: June 11, 2012     Points: 20   

how to decrypt password in PDB file? (need registration code to view the PDB file)
how to generate the reg code using isilo User ID?



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend