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

encrypt/decrypt 2 errors

I found an encrypt/decrypt example and am trying to get it to work, but get several errors.


error 1: the encrypted value come back as: String reference not set to an instance of a String. Parameter name: s


error 2: when I uncomment the lblDecrypted.Text = DecryptText(strText) the result is:
Object reference not set to an instance of an object.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 49:         Dim inputByteArray(strText.Length) As Byte


1. here is my web form contents (my.aspx):
---
            <form id="Form1" method="post" runat="server">
                  <asp:TextBox id="txtPassword" style="Z-INDEX: 101; LEFT: 32px; POSITION: absolute; TOP: 56px"
                        runat="server"></asp:TextBox>
                  <asp:Button id="btnSubmit" style="Z-INDEX: 102; LEFT: 40px; POSITION: absolute; TOP: 88px" runat="server"
                        Text="Submit"></asp:Button>
                  <asp:Label id="lblPassword" style="Z-INDEX: 103; LEFT: 32px; POSITION: absolute; TOP: 24px"
                        runat="server">Enter a Password</asp:Label>
                  <HR style="Z-INDEX: 104; LEFT: 16px; POSITION: absolute; TOP: 136px" width="100%" SIZE="1">
                  <TABLE id="Table1" style="Z-INDEX: 105; LEFT: 40px; POSITION: absolute; TOP: 168px" cellSpacing="1"
                        cellPadding="1" width="300" border="1">
                        <TR>
                              <TD style="WIDTH: 118px">Encrypted Value</TD>
                              <TD style="WIDTH: 25px"></TD>
                              <TD>
                                    <asp:Label id="lblEncrypted" runat="server"></asp:Label></TD>
                        </TR>
                        <TR>
                              <TD style="WIDTH: 118px"></TD>
                              <TD style="WIDTH: 25px"></TD>
                              <TD></TD>
                        </TR>
                        <TR>
                              <TD style="WIDTH: 118px">Decrypted Value</TD>
                              <TD style="WIDTH: 25px"></TD>
                              <TD>
                                    <asp:Label id="lblDecrypted" runat="server"></asp:Label></TD>
                        </TR>
                  </TABLE>
                  <asp:Label id="lblError" style="Z-INDEX: 106; LEFT: 256px; POSITION: absolute; TOP: 32px" runat="server"></asp:Label>
            </form>
---

2. Click submit button - code behind(my.aspx.vb):
---
Imports System
Imports System.IO
Imports System.Xml
Imports System.Text
Imports System.Security.Cryptography
Imports test_encrypt.Utilities

Public Class _default
    Inherits System.Web.UI.Page
    Public strText As String

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim strValueS As String
        strValueS = txtPassword.Text
        'strValueS = ""
        If strValueS = "" Then
            lblError.Visible = True
            lblError.Text = "Please try again as there was an error processing your request."
        Else
            EncryptText(strValueS)
            lblEncrypted.Text = EncryptText(strText)
            lblDecrypted.Text = DecryptText(strText)
        End If
    End Sub
---

3. Class file
test_encrypt.Utilities file contents (utilities.vb):
---
Imports System
Imports System.IO
Imports System.Xml
Imports System.Text
Imports System.Security.Cryptography

Public Class Utilities
    'Encrypt the text
    Public Shared Function EncryptText(ByVal strText As String) As String
        Return Encrypt(strText, "&%#@?,:*")
    End Function


    'Decrypt the text
    Public Shared Function DecryptText(ByVal strText As String) As String
        Return Decrypt(strText, "&%#@?,:*")
    End Function


    'The function used to encrypt the text
    Private Shared 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))
            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.ToString
        End Try

    End Function

    'The function used to decrypt the text
    Private Shared 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
End Class
---
0
matthersjr
Asked:
matthersjr
1 Solution
 
bobbit31Commented:
you never set the value of strText:

            EncryptText(strValueS)
            lblEncrypted.Text = EncryptText(strText) <--- strText is null
            lblDecrypted.Text = DecryptText(strText) <--- strText is null
0
 
matthersjrAuthor Commented:
I changed it to:

            'EncryptText(strValueS)
            lblEncrypted.Text = EncryptText(strValueS)
            lblDecrypted.Text = DecryptText(strValueS)

I noticed that I did not need to call EncryptText(strValueS) twice.


Now when I submit the form.
encrypted = OtAuuaEYIQTCloCsxUWLzg==
decrypted = Length of the data to decrypt is invalid.

how should I correct the decrypted error
0
 
Jeff CertainCommented:
EncryptText(strValueS)
lblEncrypted.Text = EncryptText(strText)
lblDecrypted.Text = DecryptText(strText)

should be:

strValueS = EncryptText(strValueS)
lblEncrypted.Text = strValueS
lblDecrypted.Text = DecryptText(strValueS)

This way, the text to be decrypted is the encrypted text from your first function call.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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