本文介绍了如何读取从xml文件加密的密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个简单的应用程序vb.net,它使用sql2008作为存储连接字符串项的bank和xml文件。密码项以xml加密保存,但是当我读取并调用xml数据打开sql数据库时,我看到这个错误 我的代码: 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.XmlImports System.IOImports System.Data.SqlClientImports System.Security.CryptographyImports System.TextPublic 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 SubEnd Class 请帮帮我 请!! please help me please!!推荐答案 请帮助我 please !! please help me please!! reader.ReadToFollowing("Pwd")output4.AppendLine(MyDStr(reader.ReadElementContentAsString())) 这篇关于如何读取从xml文件加密的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 18:19