I am new to VB.Net. I am trying to read a text file, encrypt it and save the encrypted text file. However when I start the encrypt process, I get a System.NullReferenceException error.
Private Sub encryptOrDecrypt(ByVal strInputFile As String, ByVal strOutputFile As String, _
ByVal byteKey() As Byte, ByVal byteInitializationVector() As Byte, _
ByVal Process As CryptoProcess)
Try
'File stream for handling file IO
fileStreamInput = New System.IO.FileStream(strInputFile, FileMode.Open, FileAccess.Read)
fileStreamOutput = New System.IO.FileStream(strOutputFile, FileMode.OpenOrCreate, FileAccess.Write)
'Ensure that output file is empty
fileStreamOutput.SetLength(0)
'Declaring variables for encryption and decryption process
Dim byteBuffer(4096) As Byte
Dim bytesProcessed As Long = 0
Dim fileLength As Long = fileStreamInput.Length
Dim intBytesInCurrentBlock As Integer
Dim csCryptoStream As CryptoStream
Dim cryptoRijnadel As New System.Security.Cryptography.RijndaelManaged
Select Case Process
Case CryptoProcess.EncryptFile
csCryptoStream = New CryptoStream(fileStreamOutput, _
cryptoRijnadel.CreateEncryptor(byteKey, byteInitializationVector), _
CryptoStreamMode.Write)
Case CryptoProcess.DecryptFile
csCryptoStream = New CryptoStream(fileStreamOutput, _
cryptoRijnadel.CreateDecryptor(byteKey, byteInitializationVector), _
CryptoStreamMode.Write)
End Select
While bytesProcessed < fileLength
intBytesInCurrentBlock = fileStreamInput.Read(byteBuffer, 0, 4096)
csCryptoStream.Write(byteBuffer, 0, intBytesInCurrentBlock)
bytesProcessed = bytesProcessed + CLng(intBytesInCurrentBlock)
End While
csCryptoStream.Close()
fileStreamInput.Close()
fileStreamOutput.Close()
If Process = CryptoProcess.EncryptFile Then
Dim fileOriginal As New FileInfo(strFileToEncrypt)
fileOriginal.Delete()
End If
Dim Wrap As String = Chr(13) + Chr(10)
If Process = CryptoProcess.EncryptFile Then
MsgBox("Done", MsgBoxStyle.Information, "Done")
End If
Catch When Err.Number = 53
MsgBox("File not found", MsgBoxStyle.Exclamation, "Invalid File")
Catch
fileStreamInput.Close()
fileStreamOutput.Close()
End Try
End Sub
The debugger shows the error at the line fileStreamOutput.Close()
Please help.