title: Dot Net Core, Cryptography, I am getting different encrypted value for same string each time
body: <p>I am trying to store and retrieve userid/password for database login from Asp.Net core application. since this is sensitive information, I have attempted encryption/decryption.</p>
  
  <p>Here is my encryption/decryption class.</p>
  
  <pre><code> public class EncryptorDecryptor
  {
  
      public static string Encrypt(string text, string key)
      {
          var _key = Encoding.UTF8.GetBytes(key);
  
          using (var aes = Aes.Create())
          {
              using (var encryptor = aes.CreateEncryptor(_key, aes.IV))
              {
                  using (var ms = new MemoryStream())
                  {
                      using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                      {
                          using (var sw = new StreamWriter(cs))
                          {
                              sw.Write(text);
                          }
                      }
  
                      var iv = aes.IV;
  
                      var encrypted = ms.ToArray();
  
                      var result = new byte[iv.Length + encrypted.Length];
  
                      Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
                      Buffer.BlockCopy(encrypted, 0, result, iv.Length, encrypted.Length);
  
                      return Convert.ToBase64String(result);
                  }
              }
          }
      }
  
      public static string Decrypt(string encrypted, string key)
      {
          var b = Convert.FromBase64String(encrypted);
  
          var iv = new byte[16];
          var cipher = new byte[16];
  
          Buffer.BlockCopy(b, 0, iv, 0, iv.Length);
          Buffer.BlockCopy(b, iv.Length, cipher, 0, iv.Length);
  
          var _key = Encoding.UTF8.GetBytes(key);
  
          using (var aes = Aes.Create())
          {
              using (var decryptor = aes.CreateDecryptor(_key, iv))
              {
                  var result = string.Empty;
                  using (var ms = new MemoryStream(cipher))
                  {
                      using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                      {
                          using (var sr = new StreamReader(cs))
                          {
                              result = sr.ReadToEnd();
                          }
                      }
                  }
  
                  return result;
              }
          }
      }
  }
  </code></pre>
  
  <p>Here is my code for consuming the encryption/decryption class</p>
  
  <pre><code> var name = "santu_ghosh";
              var pword = "p@ssword";
               string key = "E546C8DF278CD5931069B522E695D4F2";
              var encnm = EncryptorDecryptor.Encrypt(name, key);
              var encpwd = EncryptorDecryptor.Encrypt(pword,key);
  </code></pre>
  
  <p>My problem is that I am getting a different value for encnm (encrypted name) and encpwd (encrypted password). As such this algorithm is unusable for my purpose.</p>
  
  <p>My requirement is simple. I want an encryption algorithm (not necessarily AES or triple DES), that gives the same output each time in Dot net core. So I will be able to save this in appsettings.json and retrieve and decrypt, and each time the application runs, I should get the same result.</p>
  
  <p>Thanks in advance.</p>