I am trying to wrap my head around encryption in .NET. I am trying to implement a simple solution in ASP.NET Core to encrypt a cookie containing some non-meaningful numbers to outsiders. I need to be able to read the cookie content again.
This example (http://www.saipanyam.net/2010/03/encrypt-query-strings.html) uses Rfc2898DeriveBytes
and RijndaelManaged
but I don't understand if Rfc2898DeriveBytes
is producing a random key tied to e.g. machine key or something else.
I need to be able to move software to a new server without managing machine keys, Redis or similar (just looking for something very simple to encrypt data).
// is this random? And if so, will I be able to read cookie contents in potentially 2 years of time?
keyGenerator = new Rfc2898DeriveBytes(ENCRYPTION_KEY, SALT);
key = keyGenerator.GetBytes(32);
// entire code from link above (clean up)
public static class MyHelper
{
private const string ENCRYPTION_KEY = "some-secret-key";
/// The salt value used to strengthen the encryption.
private readonly static byte[] SALT = Encoding.ASCII.GetBytes(ENCRYPTION_KEY);
private readonly static byte[] key;
private readonly static byte[] iv;
private static readonly Rfc2898DeriveBytes keyGenerator;
static MyHelper()
{
keyGenerator = new Rfc2898DeriveBytes(ENCRYPTION_KEY, SALT);
key = keyGenerator.GetBytes(32);
iv = keyGenerator.GetBytes(16);
}
/// Encrypts any string using the Rijndael algorithm.
/// A Base64 encrypted string.
public static string Encrypt(string inputText)
{
//Create a new RijndaelManaged cipher for the symmetric algorithm from the key and iv
RijndaelManaged rijndaelCipher = new RijndaelManaged { Key = key, IV = iv };
byte[] plainText = Encoding.Unicode.GetBytes(inputText);
using (ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor())
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainText, 0, plainText.Length);
cryptoStream.FlushFinalBlock();
return Convert.ToBase64String(memoryStream.ToArray());
}
}
}
}
/// Decrypts a previously encrypted string.
/// The encrypted string to decrypt.
public static string Decrypt(string inputText)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
byte[] encryptedData = Convert.FromBase64String(inputText);
using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(key, iv))
{
using (MemoryStream memoryStream = new MemoryStream(encryptedData))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
byte[] plainText = new byte[encryptedData.Length];
int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
return Encoding.Unicode.GetString(plainText, 0, decryptedCount);
}
}
}
}
}