如何读取从xml文件加密的密码 [英] how to read password encrypted from xml file
本文介绍了如何读取从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.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!!
推荐答案
KaJcHeQ!
公共 功能 MyDStr( ByVal sQueryString 作为 字符串)正如 字符串
Dim buffer()作为 字节
Dim loCryptoClass 作为 新 TripleDESCryptoServiceProvider
Dim loCryptoProvider 作为 新 MD5CryptoServiceProvider
尝试
buffer =转换.FromBase64String(sQueryString)
loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII。 GetBytes(lscryptoKey))
loCryptoClass.IV = lbtVector
返回 Encoding.ASCII.GetString(loCryptoClass.CreateDecryptor()。TransformFinalBlock(buffer,< span class =code-digit> 0 ,buffer.Length()))
Catch ex 作为例外
投掷 ex
最后
loCryptoClass.Clear()
loCryptoProvider.Clear()
loCryptoClass = Nothing
loCryptoProvider = 没有
结束 尝试
结束 功能
公共 功能 MyESt r( ByVal sInputVal As String )< span class =code-keyword> As String
Dim loCryptoClass < span class =code-keyword>作为 新 TripleDESCryptoServiceProvider
Dim loCryptoProvider 作为 新 MD5CryptoServiceProvider
Dim lbtBuffer()作为 字节
尝试
lbtBuffer = System.Text.Encoding.ASCII.GetBytes(sInputVal)
loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(lscryptoKey))
loCryptoClass.IV = lbtVector
sInputVal = Co nvert.ToBase64String(loCryptoClass.CreateEncryptor()。TransformFinalBlock(lbtBuffer, 0 ,lbtBuffer.Length()))
MyEStr = sInputVal
Catch ex As CryptographicException
投掷 ex
Catch ex As FormatException
抛出 ex
Catch ex As 例外
投掷 ex
最后
loCryptoClass.Clear()
loCryptoProvider.Clear()
loCryptoClass = 没什么
loCryptoProvider = 没什么
结束 尝试
结束 函数
私有 Sub Button2_Click(< span class =code-keyword> ByVal sender As System。 Object , ByVal e As System.EventArgs) Handles Button2。点击
Dim output1 As StringBuilder = 新 StringBuilder()
Dim output2 As StringBuilder = 新 StringBuilder()
Dim output3 As StringBuilder =New StringBuilder()
Dim output4 As StringBuilder = 新 StringBuilder()
Dim xmlString 作为 字符串
Dim sti 作为 字符串
sti = Application.StartupPath + \XMLCn.xml
xmlString = IO.File.ReadAllText(sti)
' 创建XmlReader
使用 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(读者。 ReadElementContentAsString())
reader.ReadToFollowing(MyDStr( pwd))
output4.AppendLine(reader.ReadElementContentAsString())
结束 使用
TextBox1.Text = output1.ToString()
TextBox2.Text = output2.ToString()
TextBox3.Text = output3.ToString()
TextBox4.Text = outpu t4.ToString()
结束 Sub
结束 班级
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 !!
please help me
please!!
你在错误的地方调用了MyDStr
函数:
You have called theMyDStr
function in the wrong place:
reader.ReadToFollowing("Pwd")
output4.AppendLine(MyDStr(reader.ReadElementContentAsString()))
这篇关于如何读取从xml文件加密的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文