Encryption and decryption of a string in C#: a simple and secure way
Hello everyone! I'm a beginner C# developer, and I have a problem with string encryption. I need to store a user's password in an encrypted form in the database, and then decrypt it during login and compare it with the entered password. I tried using simple algorithms, but the code turns out to be cumbersome and, as I understand, insecure.
Here's what I wrote:
```csharp
public static string Encrypt(string text)
{
byte[] data = Encoding.UTF8.GetBytes(text);
for (int i = 0; i < data.Length; i++)
{
data[i] = (byte)(data[i] ^ 0xFF); // simple XOR
}
return Convert.ToBase64String(data);
}
```
But I understand that this is "child's play" in terms of security. I've heard about AES, but I don't know how to properly use it with salt, initialization vector (IV), and key.