title: Simple function to encrypt and decrypt a script
body: <p>I have two functions that are supposed to encrypt and decrypt a string but the encrypt function returns a different encrypted string for the same text everytime.  What have I done wrong?</p>
  
  <pre><code>Public Shared Function Encrypt(ByVal strText As String, ByVal strKey As String) As String
      Dim IV() As Byte = {&amp;H12, &amp;H34, &amp;H56, &amp;H78, &amp;H90, &amp;HAB, &amp;HCD, &amp;HEF}
      Try
          Dim bykey() As Byte = System.Text.Encoding.UTF8.GetBytes(Left(strKey, 8))
          Dim InputByteArray() As Byte = System.Text.Encoding.UTF8.GetBytes(strText)
          Dim des As New DESCryptoServiceProvider
          Dim ms As New IO.MemoryStream
          Dim cs As New CryptoStream(ms, des.CreateEncryptor(bykey, IV), CryptoStreamMode.Write)
          cs.Write(InputByteArray, 0, InputByteArray.Length)
          cs.FlushFinalBlock()
          Return Convert.ToBase64String(ms.ToArray())
      Catch ex As Exception
          Return ex.Message
      Finally
      End Try
  End Function
  
  Public Shared Function Decrypt(ByVal strText As String, ByVal strKey As String) As String
      Dim IV() As Byte = {&amp;H12, &amp;H34, &amp;H56, &amp;H78, &amp;H90, &amp;HAB, &amp;HCD, &amp;HEF}
      Dim inputByteArray(strText.Length) As Byte
      Try
          Dim byKey() As Byte = System.Text.Encoding.UTF8.GetBytes(Left(strKey, 8))
          Dim des As New DESCryptoServiceProvider
          inputByteArray = Convert.FromBase64String(strText)
          Dim ms As New IO.MemoryStream
          Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)
          cs.Write(inputByteArray, 0, inputByteArray.Length)
          cs.FlushFinalBlock()
          Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
          Return encoding.GetString(ms.ToArray())
      Catch ex As Exception
          Return ex.Message
      End Try
  End Function
  </code></pre>