I want to form a query in SQL.
My Requirement is-
I have a table - (Table Name - Users)
| ID | UserName | Password | EncryptedPassword | Location | IsActive |
| 1 | abc | abc | 0x7890000 | US | 1 |
| 2 | def | def | 0x88776655 | UK | 1 |
| 3 | ghi | ghi | 0x1122334455 | India | 1 |
| 4 | jkl | jkl | 0x987654321234 | Australia | 1 |
In EncryptedPassword field, for inserting data, I am passing PWDENCRYPT(@Password).
This has some varbinary values.
I want to fire a select query for checking whether the username and password are correct or not.
My query is-
select Username, PWDCOMPARE(Password, PWDENCRYPT(EncryptPassword)) PasswordCompare, Location from Users where IsActive=1
Here, even if I pass correct password, I get the PasswordCompare field as 0.
How to encrypt and decrypt passwords in SQL Server??
Please help.

Joginder BangerRank #360 · 7K reputationPosted Dec 5, 2014, 9:55 AM EST
try link have two function one is encrypt and another decrypt.
link
Manish Kumar ChoudharyRank #339 · 7.4K reputationPosted Dec 5, 2014, 6:40 PM EST
public class EncryptDecrept
{
#region Encrypt Password
///
/// This function is used for Encrypt password for user.
///
///
public static string Encrypt(string clearText)
{
string EncryptionKey = ConfigurationManager.AppSettings["Keywil"].ToString();
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
#endregion
#region Decrypt Password
///
/// This function is used for Decrypt password for user.
///
///
public static string Decrypt(string cipherText)
{
string EncryptionKey = ConfigurationManager.AppSettings["Keywil"].ToString();
cipherText = cipherText.Replace(" ", "+");
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
#endregion
}
add it in web.config
Riddhi ValechaRank #794 · 2.9K reputationPosted Dec 5, 2014, 7:34 PM EST
My target is to keep the passwords in UserMaster table in encrypted format.
I tried using PWDENCRYPT and PWDCOMPARE, but I failed in using them.
Manish Kumar ChoudharyRank #339 · 7.4K reputationPosted Dec 5, 2014, 7:44 PM EST
use above class EncryptDecrept.Encrypt(
Try this it will work .
Riddhi ValechaRank #794 · 2.9K reputationPosted Dec 5, 2014, 9:54 PM EST
I am getting an error at line - using (Aes encryptor = Aes.Create())
Please explain what is Aes encryter.
Manish Kumar ChoudharyRank #339 · 7.4K reputationPosted Dec 5, 2014, 10:35 PM EST
using System.Security.Cryptography;
on the top of ur page.