title: Different encryption is created for the same value
body: <p>I have used below code to encrypt my value. However, I noticed that for the same value new encryption format is generated instead of same encryption value. Can anyone help me to solve this issue?</p>
  <p>Example:
  Value is HelloWorld123$
  When I executed for the first time, I am getting this encryption - EAAAAE+WzLTCsNOJSQBuTwnRsfrRxqLa6WLVr0zWQ8eozkr1</p>
  <p>When I executed for the second time, I am getting this encryption - EAAAAEJuBne0limVQ4aQij89v2SjU8eHasyDlnsGGQ1MD43V</p>
  <p><strong>Question: How can I solve to get same encryption all time for same value?</strong></p>
  <pre><code>  private static byte[] _salt = { 1, 2, 3, 4, 5, 6, 7, 8 }; // Array of numbers
      internal static byte[] key = { 0x0A, 01, 02, 0x48 };
  
  
      /// &lt;summary&gt;
      /// Encrypt the given string using AES.  The string can be decrypted using 
      /// DecryptStringAES().  The sharedSecret parameters must match.
      /// &lt;/summary&gt;
      /// &lt;param name=&quot;plainText&quot;&gt;The text to encrypt.&lt;/param&gt;
      /// &lt;param name=&quot;sharedSecret&quot;&gt;A password used to generate a key for encryption.&lt;/param&gt;
      private static string EncryptStringAES(string plainText, string sharedSecret)
      {
          if (string.IsNullOrEmpty(plainText))
              throw new ArgumentNullException(&quot;plainText&quot;);
          //if (string.IsNullOrEmpty(sharedSecret))
          //    throw new ArgumentNullException(&quot;sharedSecret&quot;);
  
          string outStr = null;          // Encrypted string to return
          RijndaelManaged aesAlg = null; // RijndaelManaged object used to encrypt the data.
  
          try
          {
              // generate the key from the shared secret and the salt
              Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);
  
              // Create a RijndaelManaged object
              aesAlg = new RijndaelManaged();
              aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
  
              // Create a decryptor to perform the stream transform.
              ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
  
              // Create the streams used for encryption.
              using (MemoryStream msEncrypt = new MemoryStream())
              {
                  // prepend the IV
                  msEncrypt.Write(BitConverter.GetBytes(aesAlg.IV.Length), 0, sizeof(int));
                  msEncrypt.Write(aesAlg.IV, 0, aesAlg.IV.Length);
                  using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                  {
                      using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                      {
                          //Write all data to the stream.
                          swEncrypt.Write(plainText);
                      }
                  }
                  outStr = Convert.ToBase64String(msEncrypt.ToArray());
              }
          }
          finally
          {
              // Clear the RijndaelManaged object.
              if (aesAlg != null)
                  aesAlg.Clear();
          }
  
          // Return the encrypted bytes from the memory stream.
          return outStr;
      }
  
      /// &lt;summary&gt;
      /// Decrypt the given string.  Assumes the string was encrypted using 
      /// EncryptStringAES(), using an identical sharedSecret.
      /// &lt;/summary&gt;
      /// &lt;param name=&quot;cipherText&quot;&gt;The text to decrypt.&lt;/param&gt;
      /// &lt;param name=&quot;sharedSecret&quot;&gt;A password used to generate a key for decryption.&lt;/param&gt;
      private static string DecryptStringAES(string cipherText, string sharedSecret)
      {
          if (string.IsNullOrEmpty(cipherText))
              throw new ArgumentNullException(&quot;cipherText&quot;);
          //if (string.IsNullOrEmpty(sharedSecret))
          //    throw new ArgumentNullException(&quot;sharedSecret&quot;);
  
          // Declare the RijndaelManaged object
          // used to decrypt the data.
          RijndaelManaged aesAlg = null;
  
          // Declare the string used to hold
          // the decrypted text.
          string plaintext = null;
  
          try
          {
              // generate the key from the shared secret and the salt
              Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);
  
              // Create the streams used for decryption.                
              byte[] bytes = Convert.FromBase64String(cipherText);
              using (MemoryStream msDecrypt = new MemoryStream(bytes))
              {
                  // Create a RijndaelManaged object
                  // with the specified key and IV.
                  aesAlg = new RijndaelManaged();
                  aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
                  // Get the initialization vector from the encrypted stream
                  aesAlg.IV = ReadByteArray(msDecrypt);
                  // Create a decrytor to perform the stream transform.
                  ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                  using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                  {
                      using (StreamReader srDecrypt = new StreamReader(csDecrypt))
  
                          // Read the decrypted bytes from the decrypting stream
                          // and place them in a string.
                          plaintext = srDecrypt.ReadToEnd();
                  }
              }
          }
          finally
          {
              // Clear the RijndaelManaged object.
              if (aesAlg != null)
                  aesAlg.Clear();
          }
  
          return plaintext;
      }
  
      private static byte[] ReadByteArray(Stream s)
      {
          byte[] rawLength = new byte[sizeof(int)];
          if (s.Read(rawLength, 0, rawLength.Length) != rawLength.Length)
          {
              throw new SystemException(&quot;Stream did not contain properly formatted byte array&quot;);
          }
  
          byte[] buffer = new byte[BitConverter.ToInt32(rawLength, 0)];
          if (s.Read(buffer, 0, buffer.Length) != buffer.Length)
          {
              throw new SystemException(&quot;Did not read byte array properly&quot;);
          }
  
          return buffer;
      }
  </code></pre>
  <p>User Case:</p>
  <p>I have a form which insert form value into database. Including some valuable items which are encrypted.
  I have another form which checks whether value exist in database, When I using some method for some lookup functions I need to compare many condition for the same value. So I am directly coparing encrypted values. But as new value is created. I am unable to compare those value</p>
  <p>Hope I am able to explain mu Use Case</p>