Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Matthew Hess114 reputationPosted Jan 14, 2025, 6:14 PM EST
Adding on to Sophia Carter's answer:
Here's one way to approach salting a hash. The idea here is you pass in the plain text password and you get back both the salt and hash which you can store for authenticating subsequent requests. Some people like to combine these two strings into one to make things just a little less obvious.
private static readonly int SALT_LENGTH = 16; public static (string Salt, string Hash) HashString(string plainText) { // generate salt bytes byte[] saltBytes = RandomNumberGenerator.GetBytes(SALT_LENGTH); // generate plan text bytes byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); // combine byte[] saltedTextBytes = new byte[plainTextBytes.Length + saltBytes.Length]; Array.Copy(saltBytes, 0, saltedTextBytes, 0, saltBytes.Length); Array.Copy(plainTextBytes, 0, saltedTextBytes, saltBytes.Length, plainTextBytes.Length); // hash byte[] hashBytes = SHA256.Create().ComputeHash(saltedTextBytes); // return the tuple with both Salt and Hash so they can be stored return (Convert.ToBase64String(saltBytes), Convert.ToBase64String(hashBytes)); }Sophia CarterRank #4,018 · 371 reputationPosted Jan 14, 2025, 12:09 PM EST
Encrypting passwords is crucial for ensuring the security of sensitive information in applications. In C#, there are several techniques you can use to encrypt passwords:
1. Hashing: Hashing is a one-way transformation that converts a password into a fixed-length string of characters. This technique is commonly used for password storage. One popular hashing algorithm is SHA256. Here is a simple example in C#:
using System; using System.Security.Cryptography; using System.Text; class Program { static void Main() { string password = "mySecretPassword"; using (SHA256 sha256Hash = SHA256.Create()) { byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(password)); StringBuilder builder = new StringBuilder(); foreach (byte b in bytes) { builder.Append(b.ToString("x2")); } string hashedPassword = builder.ToString(); Console.WriteLine("Hashed Password: " + hashedPassword); } } }2. Symmetric Encryption: Symmetric encryption uses the same key for both encryption and decryption. In C#, you can use classes like `AesManaged` for symmetric encryption. Here is a simple example:
using System; using System.Security.Cryptography; using System.Text; class Program { static void Main() { string password = "mySecretPassword"; string key = "mySecretKey12345"; using (AesManaged aes = new AesManaged()) { aes.Key = Encoding.UTF8.GetBytes(key); aes.IV = Encoding.UTF8.GetBytes(key.Substring(0, 16)); ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV); byte[] encryptedBytes; using (var ms = new System.IO.MemoryStream()) { using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) { using (var sw = new System.IO.StreamWriter(cs)) { sw.Write(password); } } encryptedBytes = ms.ToArray(); } string encryptedPassword = Convert.ToBase64String(encryptedBytes); Console.WriteLine("Encrypted Password: " + encryptedPassword); } } }3. Asymmetric Encryption: Asymmetric encryption involves a public-private key pair. C# provides classes like `RSACryptoServiceProvider` for asymmetric encryption. Here's a basic example:
using System; using System.Security.Cryptography; using System.Text; class Program { static void Main() { string password = "mySecretPassword"; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { byte[] encryptedBytes = rsa.Encrypt(Encoding.UTF8.GetBytes(password), true); string encryptedPassword = Convert.ToBase64String(encryptedBytes); Console.WriteLine("Encrypted Password: " + encryptedPassword); } } }These are just a few techniques to encrypt passwords in C#. Each technique has its own use case depending on the security requirements of your application. Remember that encryption is just one part of securing passwords; always follow best practices for password management and storage.