I made this class to encrypt my DataSet before saving it
to disk. So, first in the main program I write the
DataSet to XML in a MemoryStream. I pass this stream to
the E_File sub, which encrypts the stream and writes it
out to the FileStream. Now, in the D_File function, I
read the encrypted file into a FileStream, then decrypt
it into a MemoryStream, which I pass back to my DataSet
and Read the XML into the DataSet. The problem I'm
having, is somewhere I'm losing the last 2 bytes of the
file, so if my DataSet is named "NewDataSet", the end of
the file looks like </NewDataSe
I'm not sure where these two bytes are being lost at, and
if anyone could point out my blunder, I can stop beating
my head into walls and keyboards!
Below is my code for the encryption class:
Imports System
Imports System.Text
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Public Class Perculate
Public Event Callback(ByVal Msg As String)
Public Event Bytes_Processed(ByVal Curr As Long,
ByVal Total As Long)
Protected IV() As Byte = New Byte() {0, 0, 0, 0, 0,
0, 0, 0}
Public Function D_File(ByVal InFile As String, ByVal
key As String) As Stream
Dim fin As New FileStream(InFile, FileMode.Open,
FileAccess.Read)
Dim _ms As New MemoryStream(fin.Length)
Dim bin(100) As Byte
Dim redlin As Long = 0
Dim totlen As Long = fin.Length
Dim len As Integer
If key.Length < 24 Then key = New String("0", 24 -
key.Length) & key
Try
Dim tdes As New TripleDESCryptoServiceProvider
()
Dim decStream As New CryptoStream(_ms, _
tdes.CreateDecryptor
(Text.ASCIIEncoding.ASCII.GetBytes(key), _
IV), _
CryptoStreamMode.Write)
RaiseEvent Callback("Decrypting")
fin.Position = 0
Do
len = fin.Read(bin, 0, bin.Length)
decStream.Write(bin, 0, len)
redlin += len
RaiseEvent Bytes_Processed(redlin, totlen)
If len = 0 Then Exit Do
Loop
Return _ms
Catch ex As CryptographicException
RaiseEvent Callback(ex.Message)
End Try
End Function
Public Sub E_File(ByVal InData As Stream, ByVal
Outfile As String, ByVal Key As String)
If File.Exists(Outfile) Then File.Delete(Outfile)
Dim fout As New FileStream(Outfile,
FileMode.CreateNew, FileAccess.Write)
fout.SetLength(0)
Dim bin(100) As Byte
Dim redlin As Long = 0
Dim totlen As Long = InData.Length + 2
Dim len As Integer
Dim hByte() As Byte = ASCIIEncoding.ASCII.GetBytes
("<?xml version=" & Chr(34) & "1.0" & Chr(34) & "
standalone=" & Chr(34) & "yes" & Chr(34) & "?>" & vbCrLf)
If Key.Length > 24 Then
RaiseEvent Callback("Key must be 24 or less
characters!")
Exit Sub
ElseIf Key.Length < 6 Then
RaiseEvent Callback("Key must be 6 or more
characters!")
Exit Sub
End If
If Key.Length < 24 Then Key = New String("0", 24 -
Key.Length) & Key
Try
Dim tdes As New TripleDESCryptoServiceProvider
()
If Not tdes.IsWeakKey
(Text.ASCIIEncoding.ASCII.GetBytes(Key)) Then
Dim encStream As New CryptoStream(fout, _
tdes.CreateEncryptor
(Text.ASCIIEncoding.ASCII.GetBytes(Key), _
IV),
_
CryptoStreamMode.Write)
RaiseEvent Callback("Encrypting")
InData.Position = 0
'Force it to write out the XML header info
encStream.Write(hByte, 0, hByte.Length)
Do
len = InData.Read(bin, 0, bin.Length)
encStream.Write(bin, 0, len)
redlin += len
RaiseEvent Bytes_Processed(redlin,
totlen)
If len = 0 Then Exit Do
Loop
encStream.Close()
Else
RaiseEvent Callback("The key is
weak!!!!!")
End If
Catch ex As CryptographicException
RaiseEvent Callback(ex.Message)
End Try
End Sub
End Class
Thanks.