I'm trying to store a hash table in an encrypted file
The encrypt appears to work (but I can't be sure until I can decrypt it)
I get an error when I try to decrypt the file
in
Public Shared Function decryptFile(ByVal sFile As String) As Hashtable
at
oHT = oSerializer.Deserialize(crStream)
I get an System.Xml.XmlException thrown
exception message "There is an invalid character in the given encoding. Line 1, position 1."
Can anybody point me in the right direction?
Cheers
Snuv
"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary
The encrypt appears to work (but I can't be sure until I can decrypt it)
I get an error when I try to decrypt the file
in
Public Shared Function decryptFile(ByVal sFile As String) As Hashtable
at
oHT = oSerializer.Deserialize(crStream)
I get an System.Xml.XmlException thrown
exception message "There is an invalid character in the given encoding. Line 1, position 1."
Can anybody point me in the right direction?
Code:
Private Shared m_TheKey(7) As Byte
'Stuff some random values into the vector:
Private Shared m_Vector() As Byte = {&H12, &H44, &H16, &HEE, &H88, &H15, &HDD, &H41}
Public Shared Sub EncryptFile(ByVal sFile As String, ByVal oObject As Object)
Dim oSerializer As New SoapFormatter
Dim oStream As Stream
Dim crStream As CryptoStream
Try
CreateKey("Bellepheron")
oStream = File.Open(sFile, FileMode.Create)
Dim des As New DESCryptoServiceProvider
crStream = New CryptoStream(oStream, des.CreateEncryptor(m_TheKey, m_Vector), CryptoStreamMode.Write)
'serialise the rsObject
oSerializer.Serialize(crStream, oObject)
Catch ex As Exception
Trace.Write(ex, ex.Message)
Finally
If Not crStream Is Nothing Then
crStream.Close()
End If
crStream = Nothing
If Not oStream Is Nothing Then
oStream.Close()
End If
oStream = Nothing
oSerializer = Nothing
oObject = Nothing
End Try
End Sub
Public Shared Function decryptFile(ByVal sFile As String) As Hashtable
Dim oSerializer As New SoapFormatter
Dim oStream As Stream
Dim oHT As New Hashtable
Dim crStream As CryptoStream
Try
If File.Exists(sFile) Then
CreateKey("Bellepheron")
oStream = File.Open(sFile, FileMode.Open)
'deserialise the file back to a hashtable
'create the Crypto object
Dim des As New DESCryptoServiceProvider
crStream = New CryptoStream(oStream, des.CreateEncryptor(m_TheKey, m_Vector), CryptoStreamMode.Read)
oHT = oSerializer.Deserialize(crStream)
End If
Catch ex As Exception
Trace.Write(ex, ex.Message)
Finally
If Not crStream Is Nothing Then
crStream.Close()
End If
crStream = Nothing
If Not oStream Is Nothing Then
oStream.Close()
End If
oStream = Nothing
oSerializer = Nothing
End Try
Return oHT
End Function
Private Shared Sub CreateKey(ByVal strKey As String)
' Byte array to hold key
Dim arrByte(32) As Byte
Dim AscEncod As New ASCIIEncoding
Dim i As Integer = 0
AscEncod.GetBytes(strKey, 0, strKey.Length, arrByte, i)
'Get the hash value of the password
Dim hashSha As New SHA1CryptoServiceProvider
Dim arrHash() As Byte = hashSha.ComputeHash(arrByte)
'put the hash value into the key
For i = 0 To 7
m_TheKey(i) = arrHash(i)
Next i
End Sub
Cheers
Snuv
"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary