L
Loren
I’m trying to encrypt and decrypt a file in vb.net. I am using the
TripleDESCryptoServiceProvider encryption found in
System.Security.Cryptography. Below is the code for my Encrypt and Decrypt
functions.
While my functions read and write files the encryption/decryption is not
working properly. My test file has an original length of 66,048 bytes. My
encrypted file ends up with 66,056 bytes … 8 bytes more than my original.
When I decrypt the encrypted file the resultant file is also 66,056 bytes in
length … obviously different than the original!
I feel like I’m missing something obvious. Does anyone have any
suggestions? Thanks for your help.
Public Function EncryptFile(ByVal SourceFilename As String, ByVal
DestinationFilename As String) As Boolean
Dim fsSourceStream As System.IO.FileStream = Nothing
Dim encryptionStream As CryptoStream = Nothing
Dim fsDestinationStream As System.IO.FileStream = Nothing
Try
' Open file streams for the file reading and writing
fsSourceStream = New System.IO.FileStream(SourceFilename,
FileMode.Open, IO.FileAccess.Read)
fsDestinationStream = New
System.IO.FileStream(DestinationFilename, FileMode.Create,
IO.FileAccess.Write)
'Bytes will be encrypted by an encryption stream
encryptionStream = New CryptoStream(fsDestinationStream, _
New
TripleDESCryptoServiceProvider().CreateEncryptor(mvarKey, mvarIV),
CryptoStreamMode.Write)
mvarBufferSize = 1024
' Create buffer
Dim fileBuffer(mvarBufferSize) As Byte
Dim accumulatedBytesRead As Integer = 0
Dim bytesRead As Integer
' read the file
' Process bytes from fsSourceStream through the encryptionStream
to the fsDestinationStream.
Do
bytesRead = fsSourceStream.Read(fileBuffer, 0, mvarBufferSize)
If (bytesRead = 0) Then Exit Do
encryptionStream.Write(fileBuffer, 0, bytesRead)
accumulatedBytesRead += bytesRead
Loop
encryptionStream.FlushFinalBlock()
Return True
Catch ex As Exception
MsgBox("Exception error occurred in EncryptFile." & vbNewLine &
vbNewLine _
& ex.Message, MsgBoxStyle.OkOnly Or
MsgBoxStyle.Information)
Return False
Finally
'close streams
If encryptionStream IsNot Nothing Then
encryptionStream.Close()
End If
If fsSourceStream IsNot Nothing Then
fsSourceStream.Close()
End If
If fsDestinationStream IsNot Nothing Then
fsDestinationStream.Close()
End If
End Try
End Function
Public Function DecryptFile(ByVal SourceFilename As String, ByVal
DestinationFilename As String) As Boolean
Dim fsSourceStream As System.IO.FileStream = Nothing
Dim decryptionStream As CryptoStream = Nothing
Dim fsDestinationStream As System.IO.FileStream = Nothing
Try
' Open file streams for the file reading and writing
fsSourceStream = New System.IO.FileStream(SourceFilename,
FileMode.Open, IO.FileAccess.Read)
fsDestinationStream = New
System.IO.FileStream(DestinationFilename, FileMode.Create,
IO.FileAccess.Write)
' Process bytes through a CryptoStream.
decryptionStream = New CryptoStream(fsSourceStream, _
New
TripleDESCryptoServiceProvider().CreateEncryptor(mvarKey, mvarIV),
CryptoStreamMode.Read)
' Create buffer
mvarBufferSize = 1024
Dim fileBuffer(mvarBufferSize) As Byte
Dim accumulatedBytesRead As Integer = 0
Dim bytesRead As Integer
' read the file
' Process bytes from fsSourceStream to fsDestinationStream.
Do
bytesRead = decryptionStream.Read(fileBuffer, 0,
mvarBufferSize)
If (bytesRead = 0) Then Exit Do
fsDestinationStream.Write(fileBuffer, 0, bytesRead)
accumulatedBytesRead += bytesRead
Loop
fsDestinationStream.Flush()
Return True
Catch ex As Exception
MsgBox("Exception error occurred in DecryptFile." & vbNewLine &
vbNewLine _
& ex.Message, MsgBoxStyle.OkOnly Or
MsgBoxStyle.Information)
Return False
Finally
'close(streams)
If decryptionStream IsNot Nothing Then
decryptionStream.Close()
End If
If fsSourceStream IsNot Nothing Then
fsSourceStream.Close()
End If
If fsDestinationStream IsNot Nothing Then
fsDestinationStream.Close()
End If
End Try
End Function
TripleDESCryptoServiceProvider encryption found in
System.Security.Cryptography. Below is the code for my Encrypt and Decrypt
functions.
While my functions read and write files the encryption/decryption is not
working properly. My test file has an original length of 66,048 bytes. My
encrypted file ends up with 66,056 bytes … 8 bytes more than my original.
When I decrypt the encrypted file the resultant file is also 66,056 bytes in
length … obviously different than the original!
I feel like I’m missing something obvious. Does anyone have any
suggestions? Thanks for your help.
Public Function EncryptFile(ByVal SourceFilename As String, ByVal
DestinationFilename As String) As Boolean
Dim fsSourceStream As System.IO.FileStream = Nothing
Dim encryptionStream As CryptoStream = Nothing
Dim fsDestinationStream As System.IO.FileStream = Nothing
Try
' Open file streams for the file reading and writing
fsSourceStream = New System.IO.FileStream(SourceFilename,
FileMode.Open, IO.FileAccess.Read)
fsDestinationStream = New
System.IO.FileStream(DestinationFilename, FileMode.Create,
IO.FileAccess.Write)
'Bytes will be encrypted by an encryption stream
encryptionStream = New CryptoStream(fsDestinationStream, _
New
TripleDESCryptoServiceProvider().CreateEncryptor(mvarKey, mvarIV),
CryptoStreamMode.Write)
mvarBufferSize = 1024
' Create buffer
Dim fileBuffer(mvarBufferSize) As Byte
Dim accumulatedBytesRead As Integer = 0
Dim bytesRead As Integer
' read the file
' Process bytes from fsSourceStream through the encryptionStream
to the fsDestinationStream.
Do
bytesRead = fsSourceStream.Read(fileBuffer, 0, mvarBufferSize)
If (bytesRead = 0) Then Exit Do
encryptionStream.Write(fileBuffer, 0, bytesRead)
accumulatedBytesRead += bytesRead
Loop
encryptionStream.FlushFinalBlock()
Return True
Catch ex As Exception
MsgBox("Exception error occurred in EncryptFile." & vbNewLine &
vbNewLine _
& ex.Message, MsgBoxStyle.OkOnly Or
MsgBoxStyle.Information)
Return False
Finally
'close streams
If encryptionStream IsNot Nothing Then
encryptionStream.Close()
End If
If fsSourceStream IsNot Nothing Then
fsSourceStream.Close()
End If
If fsDestinationStream IsNot Nothing Then
fsDestinationStream.Close()
End If
End Try
End Function
Public Function DecryptFile(ByVal SourceFilename As String, ByVal
DestinationFilename As String) As Boolean
Dim fsSourceStream As System.IO.FileStream = Nothing
Dim decryptionStream As CryptoStream = Nothing
Dim fsDestinationStream As System.IO.FileStream = Nothing
Try
' Open file streams for the file reading and writing
fsSourceStream = New System.IO.FileStream(SourceFilename,
FileMode.Open, IO.FileAccess.Read)
fsDestinationStream = New
System.IO.FileStream(DestinationFilename, FileMode.Create,
IO.FileAccess.Write)
' Process bytes through a CryptoStream.
decryptionStream = New CryptoStream(fsSourceStream, _
New
TripleDESCryptoServiceProvider().CreateEncryptor(mvarKey, mvarIV),
CryptoStreamMode.Read)
' Create buffer
mvarBufferSize = 1024
Dim fileBuffer(mvarBufferSize) As Byte
Dim accumulatedBytesRead As Integer = 0
Dim bytesRead As Integer
' read the file
' Process bytes from fsSourceStream to fsDestinationStream.
Do
bytesRead = decryptionStream.Read(fileBuffer, 0,
mvarBufferSize)
If (bytesRead = 0) Then Exit Do
fsDestinationStream.Write(fileBuffer, 0, bytesRead)
accumulatedBytesRead += bytesRead
Loop
fsDestinationStream.Flush()
Return True
Catch ex As Exception
MsgBox("Exception error occurred in DecryptFile." & vbNewLine &
vbNewLine _
& ex.Message, MsgBoxStyle.OkOnly Or
MsgBoxStyle.Information)
Return False
Finally
'close(streams)
If decryptionStream IsNot Nothing Then
decryptionStream.Close()
End If
If fsSourceStream IsNot Nothing Then
fsSourceStream.Close()
End If
If fsDestinationStream IsNot Nothing Then
fsDestinationStream.Close()
End If
End Try
End Function