Click here to Skip to main content
15,867,306 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi i have a simple app vb.net that use sql2008 as bank and xml file that save connection string items . the password item saved encrypted in xml but when i will read and call xml data to open sql database i see this error
my code :

VB
Imports System.Xml
Imports System.IO
Imports System.Data.SqlClient
Imports System.Security.Cryptography
Imports System.Text
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim rezaetiDataSet As New DataSet()
        If Not File.Exists(Application.StartupPath + "\XMLCn.xml") Then
            If Test_Connection(Txtserver.Text, Txtdatabase.Text, Txtuid.Text, Txtpwd.Text) > 0 Then
                Create_Xml()
                MsgBox(" اconnection is open")
            End If
            Exit Sub
        End If
        rezaetiDataSet.ReadXml(Application.StartupPath + "\XMLCn.xml")
        If Test_Connection(rezaetiDataSet.Tables(0).Rows(0).Item("ServerName"), rezaetiDataSet.Tables(0).Rows(0).Item("DataBase"), rezaetiDataSet.Tables(0).Rows(0).Item("Uid"), MyDStr(rezaetiDataSet.Tables(0).Rows(0).Item("Pwd"))) = 0 Then
            File.Delete(Application.StartupPath + "\XMLCn.xml")

        Else
            MsgBox(" اconnection is open")

        End If
        Exit Sub
    End Sub
    Function Test_Connection(ByVal s As String, ByVal d As String, ByVal u As String, ByVal p As String)
        Test_Connection = 1
        Dim St As String = ""
        St = String.Format("Server ={0} ;DataBase={1} ;Uid={2} ;Pwd={3}", s, d, u, p)
        Dim Cn As New SqlConnection(St)
        Try
            Cn.Open()
        Catch ex As SqlClient.SqlException
            If ex.Number <> 0 Then
                MsgBox(" اconnection is not open")")
                Test_Connection = 0
                Exit Function
            Else
                 MsgBox(" اconnection is open")")

                Exit Function
            End If
        End Try
    End Function

    Private Sub createNode(ByVal writer As XmlTextWriter)
        writer.WriteStartElement("Cn")
        writer.WriteStartElement("ServerName")
        writer.WriteString(Txtserver.Text)
        writer.WriteEndElement()
        writer.WriteStartElement("DataBase")
        writer.WriteString(Txtdatabase.Text)
        writer.WriteEndElement()
        writer.WriteStartElement("Uid")
        writer.WriteString(Txtuid.Text)
        writer.WriteEndElement()
        writer.WriteStartElement("Pwd")
        writer.WriteString(MyEStr(Txtpwd.Text))
        writer.WriteEndElement()
        writer.WriteEndElement()
    End Sub

    Private Sub Create_Xml()
        Dim writer As New XmlTextWriter("XMLCn.xml", System.Text.Encoding.UTF8)
        writer.WriteStartDocument(True)
        writer.Formatting = Formatting.Indented
        writer.Indentation = 2
        writer.WriteStartElement("Connection")
        createNode(writer)
        writer.WriteEndElement()
        writer.WriteEndDocument()
        writer.Close()
    End Sub

    Private lbtVector() As Byte = {140, 8, 85, 29, 0, 77, 193, 51}
    Private lscryptoKey As String = "$KaJcHeQ!"

    Public Function MyDStr(ByVal sQueryString As String) As String
        Dim buffer() As Byte
        Dim loCryptoClass As New TripleDESCryptoServiceProvider
        Dim loCryptoProvider As New MD5CryptoServiceProvider
        Try
            buffer = Convert.FromBase64String(sQueryString)
            loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(lscryptoKey))
            loCryptoClass.IV = lbtVector
            Return Encoding.ASCII.GetString(loCryptoClass.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length()))
        Catch ex As Exception
            Throw ex
        Finally
            loCryptoClass.Clear()
            loCryptoProvider.Clear()
            loCryptoClass = Nothing
            loCryptoProvider = Nothing
        End Try
    End Function

    Public Function MyEStr(ByVal sInputVal As String) As String
        Dim loCryptoClass As New TripleDESCryptoServiceProvider
        Dim loCryptoProvider As New MD5CryptoServiceProvider
        Dim lbtBuffer() As Byte
        Try
            lbtBuffer = System.Text.Encoding.ASCII.GetBytes(sInputVal)
            loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(lscryptoKey))
            loCryptoClass.IV = lbtVector
            sInputVal = Convert.ToBase64String(loCryptoClass.CreateEncryptor().TransformFinalBlock(lbtBuffer, 0, lbtBuffer.Length()))
            MyEStr = sInputVal
        Catch ex As CryptographicException
            Throw ex
        Catch ex As FormatException
            Throw ex
        Catch ex As Exception
            Throw ex
        Finally
            loCryptoClass.Clear()
            loCryptoProvider.Clear()
            loCryptoClass = Nothing
            loCryptoProvider = Nothing
        End Try
    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim output1 As StringBuilder = New StringBuilder()
        Dim output2 As StringBuilder = New StringBuilder()
        Dim output3 As StringBuilder = New StringBuilder()
        Dim output4 As StringBuilder = New StringBuilder()
        Dim xmlString As String
        Dim sti As String
        sti = Application.StartupPath + "\XMLCn.xml"
        xmlString = IO.File.ReadAllText(sti)
        ' Create an XmlReader
        Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))
            reader.ReadToFollowing("ServerName")
            output1.AppendLine(reader.ReadElementContentAsString())
            reader.ReadToFollowing("DataBase")
            output2.AppendLine(reader.ReadElementContentAsString())
            reader.ReadToFollowing("Uid")
            output3.AppendLine(reader.ReadElementContentAsString())
            reader.ReadToFollowing(MyDStr("pwd"))
            output4.AppendLine(reader.ReadElementContentAsString())
        End Using
        TextBox1.Text = output1.ToString()
        TextBox2.Text = output2.ToString()
        TextBox3.Text = output3.ToString()
        TextBox4.Text = output4.ToString()
    End Sub
End Class


please help me
please!!
Posted
Updated 13-Dec-20 0:40am
v3
Comments
rezaeti 7-Aug-15 13:13pm    
my friends!
pleas help me
i must propose my project tomarrow
PIEBALDconsult 7-Aug-15 13:16pm    
Not our problem.
Use the debugger to check the values as you go and detect which statement throws the Exception.
rezaeti 7-Aug-15 13:53pm    
this line:
Catch ex As Exception
Throw ex
error is :
Invalid length for a Base-64 char array.
rezaeti 7-Aug-15 13:56pm    
my deer PIEBALDconsult
thanks for reply my error is :
Invalid length for a Base-64 char array.
PIEBALDconsult 7-Aug-15 14:01pm    
BUT WHICH STATEMENT THROWS IT?!

1 solution

You have called the MyDStr function in the wrong place:
VB.NET
reader.ReadToFollowing("Pwd")
output4.AppendLine(MyDStr(reader.ReadElementContentAsString()))
 
Share this answer
 
v2
Comments
rezaeti 7-Aug-15 14:48pm    
i change place of function but this error thrown
The ReadElementContentAsString method is not supported on node type None. Line 9, position 14.
thanks for reply
please help me my friend
Richard Deeming 7-Aug-15 14:50pm    
Correct the case of the element name. XML is case-sensitive.

Your create method creates an element called "Pwd", but your read method is looking for an element called "pwd".

reader.ReadToFollowing("Pwd")
rezaeti 7-Aug-15 14:54pm    
very very very thanks
rezaeti 7-Aug-15 14:55pm    
really thanks my friend you make me INDEBTED
rezaeti 7-Aug-15 14:56pm    
Richard Deeming thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900