title: Encryption of email address in vb.net
body: <p>I would like to know how I can encrypt an email address via vb.net code. </p>
  
  <p>I found one sample which doesn't quite work with special characters and I am getting this error: </p>
  
  <p><code>The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters.</code></p>
  
  <p>Here is the code I am trying:</p>
  
  <pre><code>  'The function used to encrypt the text
      Private Function Encrypt(ByVal strText As String, ByVal strEncrKey _
               As String) As String
          Dim byKey() As Byte = {}
          Dim IV() As Byte = {&amp;H12, &amp;H34, &amp;H56, &amp;H78, &amp;H90, &amp;HAB, &amp;HCD, &amp;HEF}
  
          Try
              byKey = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8))
  
              Dim des As New DESCryptoServiceProvider()
              Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(strText)
              Dim ms As New 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
          End Try
  
      End Function
  </code></pre>
  
  <p>What do you guys think? What am I doing wrong?
  Thanks, Laziale</p>
  
  <p>UPDATE: Full Stack trace:</p>
  
  <p>System.FormatException was caught
    Message=The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters. 
    Source=mscorlib
    StackTrace:
         at System.Convert.FromBase64String(String s)
         at WEbsite.Login.Decrypt(String strText, String sDecrKey) in D:\Website\Account\Login.aspx.vb:line 213
    InnerException: </p>
  
  <p>UPDATE 2: </p>
  
  <p>Encryption method added:</p>
  
  <p>'The function used to decrypt the text</p>
  
  <pre><code>  Private Function Decrypt(ByVal strText As String, ByVal sDecrKey _
                 As String) As String
          Dim byKey() As Byte = {}
          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
              byKey = System.Text.Encoding.UTF8.GetBytes(Left(sDecrKey, 8))
              Dim des As New DESCryptoServiceProvider()
              inputByteArray = Convert.FromBase64String(strText)
              Dim ms As New 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>