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.
Manish Kumar ChoudharyPosted Dec 6, 2014, 3:35 AM
using System.Security.Cryptography;
on the top of ur page.
Riddhi ValechaPosted Dec 6, 2014, 2:54 AM
I am getting an error at line - using (Aes encryptor = Aes.Create())
Please explain what is Aes encryter.
Manish Kumar ChoudharyPosted Dec 6, 2014, 12:44 AM
use above class EncryptDecrept.Encrypt(
Try this it will work .
Riddhi ValechaPosted Dec 6, 2014, 12:34 AM
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 ChoudharyPosted Dec 5, 2014, 11:40 PM
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
Joginder BangerPosted Dec 5, 2014, 2:55 PM
try link have two function one is encrypt and another decrypt.
link