I’m trying to encrypt and decrypt a file in vb.net. I am using the
TripleDESCrypto ServiceProvider 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(ByV al SourceFilename As String, ByVal
DestinationFile name As String) As Boolean
Dim fsSourceStream As System.IO.FileS tream = Nothing
Dim encryptionStrea m As CryptoStream = Nothing
Dim fsDestinationSt ream As System.IO.FileS tream = Nothing
Try
' Open file streams for the file reading and writing
fsSourceStream = New System.IO.FileS tream(SourceFil ename,
FileMode.Open, IO.FileAccess.R ead)
fsDestinationSt ream = New
System.IO.FileS tream(Destinati onFilename, FileMode.Create ,
IO.FileAccess.W rite)
'Bytes will be encrypted by an encryption stream
encryptionStrea m = New CryptoStream(fs DestinationStre am, _
New
TripleDESCrypto ServiceProvider ().CreateEncryp tor(mvarKey, mvarIV),
CryptoStreamMod e.Write)
mvarBufferSize = 1024
' Create buffer
Dim fileBuffer(mvar BufferSize) As Byte
Dim accumulatedByte sRead As Integer = 0
Dim bytesRead As Integer
' read the file
' Process bytes from fsSourceStream through the encryptionStrea m
to the fsDestinationSt ream.
Do
bytesRead = fsSourceStream. Read(fileBuffer , 0, mvarBufferSize)
If (bytesRead = 0) Then Exit Do
encryptionStrea m.Write(fileBuf fer, 0, bytesRead)
accumulatedByte sRead += bytesRead
Loop
encryptionStrea m.FlushFinalBlo ck()
Return True
Catch ex As Exception
MsgBox("Excepti on error occurred in EncryptFile." & vbNewLine &
vbNewLine _
& ex.Message, MsgBoxStyle.OkO nly Or
MsgBoxStyle.Inf ormation)
Return False
Finally
'close streams
If encryptionStrea m IsNot Nothing Then
encryptionStrea m.Close()
End If
If fsSourceStream IsNot Nothing Then
fsSourceStream. Close()
End If
If fsDestinationSt ream IsNot Nothing Then
fsDestinationSt ream.Close()
End If
End Try
End Function
Public Function DecryptFile(ByV al SourceFilename As String, ByVal
DestinationFile name As String) As Boolean
Dim fsSourceStream As System.IO.FileS tream = Nothing
Dim decryptionStrea m As CryptoStream = Nothing
Dim fsDestinationSt ream As System.IO.FileS tream = Nothing
Try
' Open file streams for the file reading and writing
fsSourceStream = New System.IO.FileS tream(SourceFil ename,
FileMode.Open, IO.FileAccess.R ead)
fsDestinationSt ream = New
System.IO.FileS tream(Destinati onFilename, FileMode.Create ,
IO.FileAccess.W rite)
' Process bytes through a CryptoStream.
decryptionStrea m = New CryptoStream(fs SourceStream, _
New
TripleDESCrypto ServiceProvider ().CreateEncryp tor(mvarKey, mvarIV),
CryptoStreamMod e.Read)
' Create buffer
mvarBufferSize = 1024
Dim fileBuffer(mvar BufferSize) As Byte
Dim accumulatedByte sRead As Integer = 0
Dim bytesRead As Integer
' read the file
' Process bytes from fsSourceStream to fsDestinationSt ream.
Do
bytesRead = decryptionStrea m.Read(fileBuff er, 0,
mvarBufferSize)
If (bytesRead = 0) Then Exit Do
fsDestinationSt ream.Write(file Buffer, 0, bytesRead)
accumulatedByte sRead += bytesRead
Loop
fsDestinationSt ream.Flush()
Return True
Catch ex As Exception
MsgBox("Excepti on error occurred in DecryptFile." & vbNewLine &
vbNewLine _
& ex.Message, MsgBoxStyle.OkO nly Or
MsgBoxStyle.Inf ormation)
Return False
Finally
'close(streams)
If decryptionStrea m IsNot Nothing Then
decryptionStrea m.Close()
End If
If fsSourceStream IsNot Nothing Then
fsSourceStream. Close()
End If
If fsDestinationSt ream IsNot Nothing Then
fsDestinationSt ream.Close()
End If
End Try
End Function
--
Loren Baker
TripleDESCrypto ServiceProvider 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(ByV al SourceFilename As String, ByVal
DestinationFile name As String) As Boolean
Dim fsSourceStream As System.IO.FileS tream = Nothing
Dim encryptionStrea m As CryptoStream = Nothing
Dim fsDestinationSt ream As System.IO.FileS tream = Nothing
Try
' Open file streams for the file reading and writing
fsSourceStream = New System.IO.FileS tream(SourceFil ename,
FileMode.Open, IO.FileAccess.R ead)
fsDestinationSt ream = New
System.IO.FileS tream(Destinati onFilename, FileMode.Create ,
IO.FileAccess.W rite)
'Bytes will be encrypted by an encryption stream
encryptionStrea m = New CryptoStream(fs DestinationStre am, _
New
TripleDESCrypto ServiceProvider ().CreateEncryp tor(mvarKey, mvarIV),
CryptoStreamMod e.Write)
mvarBufferSize = 1024
' Create buffer
Dim fileBuffer(mvar BufferSize) As Byte
Dim accumulatedByte sRead As Integer = 0
Dim bytesRead As Integer
' read the file
' Process bytes from fsSourceStream through the encryptionStrea m
to the fsDestinationSt ream.
Do
bytesRead = fsSourceStream. Read(fileBuffer , 0, mvarBufferSize)
If (bytesRead = 0) Then Exit Do
encryptionStrea m.Write(fileBuf fer, 0, bytesRead)
accumulatedByte sRead += bytesRead
Loop
encryptionStrea m.FlushFinalBlo ck()
Return True
Catch ex As Exception
MsgBox("Excepti on error occurred in EncryptFile." & vbNewLine &
vbNewLine _
& ex.Message, MsgBoxStyle.OkO nly Or
MsgBoxStyle.Inf ormation)
Return False
Finally
'close streams
If encryptionStrea m IsNot Nothing Then
encryptionStrea m.Close()
End If
If fsSourceStream IsNot Nothing Then
fsSourceStream. Close()
End If
If fsDestinationSt ream IsNot Nothing Then
fsDestinationSt ream.Close()
End If
End Try
End Function
Public Function DecryptFile(ByV al SourceFilename As String, ByVal
DestinationFile name As String) As Boolean
Dim fsSourceStream As System.IO.FileS tream = Nothing
Dim decryptionStrea m As CryptoStream = Nothing
Dim fsDestinationSt ream As System.IO.FileS tream = Nothing
Try
' Open file streams for the file reading and writing
fsSourceStream = New System.IO.FileS tream(SourceFil ename,
FileMode.Open, IO.FileAccess.R ead)
fsDestinationSt ream = New
System.IO.FileS tream(Destinati onFilename, FileMode.Create ,
IO.FileAccess.W rite)
' Process bytes through a CryptoStream.
decryptionStrea m = New CryptoStream(fs SourceStream, _
New
TripleDESCrypto ServiceProvider ().CreateEncryp tor(mvarKey, mvarIV),
CryptoStreamMod e.Read)
' Create buffer
mvarBufferSize = 1024
Dim fileBuffer(mvar BufferSize) As Byte
Dim accumulatedByte sRead As Integer = 0
Dim bytesRead As Integer
' read the file
' Process bytes from fsSourceStream to fsDestinationSt ream.
Do
bytesRead = decryptionStrea m.Read(fileBuff er, 0,
mvarBufferSize)
If (bytesRead = 0) Then Exit Do
fsDestinationSt ream.Write(file Buffer, 0, bytesRead)
accumulatedByte sRead += bytesRead
Loop
fsDestinationSt ream.Flush()
Return True
Catch ex As Exception
MsgBox("Excepti on error occurred in DecryptFile." & vbNewLine &
vbNewLine _
& ex.Message, MsgBoxStyle.OkO nly Or
MsgBoxStyle.Inf ormation)
Return False
Finally
'close(streams)
If decryptionStrea m IsNot Nothing Then
decryptionStrea m.Close()
End If
If fsSourceStream IsNot Nothing Then
fsSourceStream. Close()
End If
If fsDestinationSt ream IsNot Nothing Then
fsDestinationSt ream.Close()
End If
End Try
End Function
--
Loren Baker
Comment