• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1159
  • Last Modified:

Encryption/Decryption Key

Hi

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
    Convert.ToBase64String(shaM.ComputeHash(Encoding.ASCII.GetBytes(Data)))
    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

</script>

<html>
      <head>
            <title></title>
      </head>
      <body>
            <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" />
            </form>
      </body>
</html>

How do I use an encryption key with this?
0
narmi2
Asked:
narmi2
1 Solution
 
raterusCommented:
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), _
                        CryptoStreamMode.Write)
                Dim sw As StreamWriter = New StreamWriter(cs)

                sw.Write(value)
                sw.Flush()
                cs.FlushFinalBlock()
                ms.Flush()

                '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), _
                        CryptoStreamMode.Read)
                Dim sr As StreamReader = New StreamReader(cs)

                Return sr.ReadToEnd()
            End If
        End Function
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now