Encryption of email address in vb.net

I would like to know how I can encrypt an email address via vb.net code.

I found one sample which doesn't quite work with special characters and I am getting this error:

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.

Here is the code I am trying:

  '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 = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &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

What do you guys think? What am I doing wrong? Thanks, Laziale

UPDATE: Full Stack trace:

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:

UPDATE 2:

Encryption method added:

'The function used to decrypt the text

  Private Function Decrypt(ByVal strText As String, ByVal sDecrKey _
               As String) As String
        Dim byKey() As Byte = {}
        Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &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

Answers


I have tried your Decrypt using, as input, the encrypted text and the same key. It works as expected. The only change I have made to your code is the use of the Substring method instead of Left as in

byKey = System.Text.Encoding.UTF8.GetBytes(strDecrKey.Substring(0, 8))

I call the two methods in this way:

Dim result as String = Encrypt("test@gmail.com", "ABCD9876") 
Dim decrypted = Decrypt(result, "ABCD9876")

I get back "test@gmail.com". -Buon Weekend anche a te-


Use an String Builder instead of string as the parameter with the special characters.

Best regards.


Need Your Help

Divide a textarea strings into groups depends on blank space

php html forms textarea

I have a textarea in my html form where user can enter numbers and after the form submit,php will echo sum of the numbers

How to viewstate in normal HTML input in asp.net

asp.net viewstate

How to use VIEWSTATE in normal HTML input elements in ASP.net. I am having a form with lot of input elements and i had normal(not asp.net input elements) input elements. Now i should i maintain vie...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.