[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

encrypt/decrypt  2 errors

Posted on 2005-04-28
3
Medium Priority
?
489 Views
Last Modified: 2012-08-14
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
Comment
Question by:matthersjr
3 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 13887435
you never set the value of strText:

            EncryptText(strValueS)
            lblEncrypted.Text = EncryptText(strText) <--- strText is null
            lblDecrypted.Text = DecryptText(strText) <--- strText is null
0
 

Author Comment

by:matthersjr
ID: 13887616
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
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 2000 total points
ID: 13887664
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month17 days, 17 hours left to enroll

829 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