I have been working on an offline windows forms application that has sign-in and signup linked to SQL Database. Storing users' passwords, email, and username to local-DB.
I have done up to this...
public static string Encrypt(string encryptString,string EncryptionKey)
{
byte[] clearBytes = Encoding.Unicode.GetBytes(encryptString);
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();
}
encryptString = Convert.ToBase64String(ms.ToArray());
}
}
return encryptString;
}
private static Random random = new Random();
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%&*+-/";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
An encryption key will be created randomly for each user while creating an account(sign-up) and it will be stored in DB for decryption(sign-in). Is this the correct way? How can I use this method and store keys in a safe way?
I have gone through some websites and blogs, But cannot understand what's the right approach to store users' data to the database and if so, in which manner or format it should be stored?
Mainly,
My confusion is should we only hash the password(stored in DB) and check with the hash generated from the user's password while logging in?.
How do big tech companies store passwords into databases?
Thanks in advance