Encryption/Decryption Key

Posted on 2005-05-09
Last Modified: 2012-05-05

I have the following code which does symmetric encryption decryption:

<%@ Page Language="VB" runat="server" debug="true" explicit="true" aspcompat=true%>
<%@ Import Namespace="System.Security.Cryptography" %>

<script language="VB" runat="server">

Public Shared Function Encrypt(ByVal Data As String) As String
    Dim shaM As New SHA1Managed
    Dim eNC_data() As Byte = ASCIIEncoding.ASCII.GetBytes(Data)
    Dim eNC_str As String = Convert.ToBase64String(eNC_data)
    Encrypt = eNC_str
End Function

Public Shared Function Decrypt(ByVal Data As String) As String
    Dim dEC_data() As Byte = Convert.FromBase64String(Data)
    Dim dEC_Str As String = ASCIIEncoding.ASCII.GetString(dEC_data)
    Decrypt = dEC_Str
End Function

Sub EncryptText(sender as object, e as eventargs)
      txtEncrypted.Text = Encrypt(TextBox1.Text)
End Sub

Sub DecryptText(sender as object, e as eventargs)
      txtEncrypted.Text = Decrypt(TextBox1.Text)
End Sub


            <form id="form1" runat="server">
                  <asp:textbox id="textbox1" runat="server" /><br />
                  <asp:button text="Encrypt" runat="server" id="btnEncrypt" OnClick="EncryptText" />
                  <asp:button text="Decrypt" runat="server" id="btnDecrypt" OnClick="DecryptText" /><br /><br />
                  <asp:textbox id="txtEncrypted" runat="server" />

How do I use an encryption key with this?
Question by:narmi2
    1 Comment
    LVL 33

    Accepted Solution

    Here's my encryption algoritms, just replace all the x's with a integer between 0-255, randomly generated would be best.

            '24 byte or 192 bit key and IV for TripleDES
            Private Shared KEY_192() As Byte = {x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x}
            Private Shared IV_192() As Byte = {x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x}

            'TRIPLE DES encryption
            Public Shared Function EncryptTripleDES(ByVal value As String) As String
                If value <> "" Then
                    Dim cryptoProvider As TripleDESCryptoServiceProvider = _
                        New TripleDESCryptoServiceProvider
                    Dim ms As MemoryStream = New MemoryStream
                    Dim cs As CryptoStream = _
                        New CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_192, IV_192), _
                    Dim sw As StreamWriter = New StreamWriter(cs)


                    'convert back to a string
                    Return Convert.ToBase64String(ms.GetBuffer(), 0, CInt(ms.Length))
                End If
            End Function

            'TRIPLE DES decryption
            Public Shared Function DecryptTripleDES(ByVal value As String) As String
                If value <> "" Then
                    Dim cryptoProvider As TripleDESCryptoServiceProvider = _
                        New TripleDESCryptoServiceProvider

                    'convert from string to byte array
                    Dim buffer As Byte() = Convert.FromBase64String(value)
                    Dim ms As MemoryStream = New MemoryStream(buffer)
                    Dim cs As CryptoStream = _
                        New CryptoStream(ms, cryptoProvider.CreateDecryptor(KEY_192, IV_192), _
                    Dim sr As StreamReader = New StreamReader(cs)

                    Return sr.ReadToEnd()
                End If
            End Function

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now