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 :
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)
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!!