Hi! I need your help! This is breaking my head! I'm new in Visual Basic 2005... I used to programme in VB6 and this is very different.. well...
I need to encrypt only a part of a file (a binary file), the first 140 kb for example... I'm trying but the resulting file (encrypted) has 16 bytes more,
This is what I want...
I have a file with text for ex. (but I want to do this with binary files, zip, exe, pdf...):
....here the 140byte
Thank you very much!!!!
I need to encrypt only a part of a file (a binary file), the first 140 kb for example... I'm trying but the resulting file (encrypted) has 16 bytes more,
This is what I want...
I have a file with text for ex. (but I want to do this with binary files, zip, exe, pdf...):
....here the 140byte
After de encryption....here the 140byte123456789012345678901234567890 (...) 01234567890123456789012345678...
I've tried with this code, I used some code I found to encrypt 100% a file, but I cant adapte it ;(FJSDK04934983FM43LF3093F303F43 (...) DJLFKF44301234567890123456789...
VB.NET:
Private Sub EncryptOrDecryptFile(ByVal strInputFile As String, _
ByVal strOutputFile As String, _
ByVal bytKey() As Byte, _
ByVal bytIV() As Byte, _
ByVal Direction As CryptoAction)
fsInput = New System.IO.FileStream(strInputFile, FileMode.Open, _
FileAccess.Read)
fsOutput = New System.IO.FileStream(strOutputFile, FileMode.OpenOrCreate, _
FileAccess.Write)
fsOutput.SetLength(0) 'make sure fsOutput is empty
'Declare variables for encrypt/decrypt process.
Dim bytBuffer(4096) As Byte 'holds a block of bytes for processing
Dim lngBytesProcessed As Long = 0 'running count of bytes processed
Dim lngFileLength As Long = fsInput.Length 'the input file's length
Dim intBytesInCurrentBlock As Integer 'current bytes being processed
Dim csCryptoStream As CryptoStream
'Declare your CryptoServiceProvider.
Dim cspRijndael As New System.Security.Cryptography.RijndaelManaged
'Setup Progress Bar
pbStatus.Value = 0
pbStatus.Maximum = 100
'Determine if ecryption or decryption and setup CryptoStream.
Select Case Direction
Case CryptoAction.ActionEncrypt
csCryptoStream = New CryptoStream(fsOutput, _
cspRijndael.CreateEncryptor(bytKey, bytIV), _
CryptoStreamMode.Write)
Case CryptoAction.ActionDecrypt
csCryptoStream = New CryptoStream(fsOutput, _
cspRijndael.CreateDecryptor(bytKey, bytIV), _
CryptoStreamMode.Write)
End Select
'Use While to loop until all of the file is processed.
Dim pfinish As Boolean
pfinish = False
Dim pcounter As Integer
pcounter = 0
While (lngBytesProcessed < lngFileLength) And (pfinish = False)
'Read file with the input filestream.
intBytesInCurrentBlock = fsInput.Read(bytBuffer, 0, 4096)
'Write output file with the cryptostream.
If (pcounter < 33) Then 'With this I only encrypt the first 33*4096 bytes.. near 140 bytes...
' it doesn't matter if its the 140 first bytes, or 100, but I mean the first bytes, to make useless the file...
csCryptoStream.Write(bytBuffer, 0, intBytesInCurrentBlock)
Else
csCryptoStream.Close()
fsInput.Close()
fsOutput.Close()
Dim leo As New System.IO.FileStream(strInputFile, FileMode.Open)
Dim matriz(CInt(lngFileLength)) As Byte
Dim matriz2(CInt(lngFileLength)) As Byte
leo.Read(matriz, 0, CInt(lngFileLength))
Dim algo As New System.IO.FileStream(strOutputFile, FileMode.Append)
algo.Write(matriz, 0, CInt(lngFileLength) - (33 * 4096))
pfinish = True
End If
'Update lngBytesProcessed
lngBytesProcessed = lngBytesProcessed + CLng(intBytesInCurrentBlock)
'Update Progress Bar
pbStatus.Value = CInt((lngBytesProcessed / lngFileLength) * 100)
contador = contador + 1
End While
'Close FileStreams and CryptoStream.
csCryptoStream.Close()
fsInput.Close()
fsOutput.Close()
'If encrypting then delete the original unencrypted file.
If Direction = CryptoAction.ActionEncrypt Then
Dim fileOriginal As New FileInfo(strFileToEncrypt)
fileOriginal.Delete()
End If
'If decrypting then delete the encrypted file.
If Direction = CryptoAction.ActionDecrypt Then
Dim fileEncrypted As New FileInfo(strFileToDecrypt)
fileEncrypted.Delete()
End If
'Update the user when the file is done.
Dim Wrap As String = Chr(13) + Chr(10)
If Direction = CryptoAction.ActionEncrypt Then
MsgBox("Encryption Complete" + Wrap + Wrap + _
"Total bytes processed = " + _
lngBytesProcessed.ToString, _
MsgBoxStyle.Information, "Done")
'Update the progress bar and textboxes.
pbStatus.Value = 0
txtFileToEncrypt.Text = "Click Browse to load file."
txtPassEncrypt.Text = ""
txtConPassEncrypt.Text = ""
txtDestinationEncrypt.Text = ""
btnChangeEncrypt.Enabled = False
btnEncrypt.Enabled = False
Else
'Update the user when the file is done.
MsgBox("Decryption Complete" + Wrap + Wrap + _
"Total bytes processed = " + _
lngBytesProcessed.ToString, _
MsgBoxStyle.Information, "Done")
'Update the progress bar and textboxes.
pbStatus.Value = 0
txtFileToDecrypt.Text = "Click Browse to load file."
txtPassDecrypt.Text = ""
txtConPassDecrypt.Text = ""
txtDestinationDecrypt.Text = ""
btnChangeDecrypt.Enabled = False
btnDecrypt.Enabled = False
End If
'Catch file not found error.
' Catch When Err.Number = 53 'if file not found
'MsgBox("Please check to make sure the path and filename" + _
' "are correct and if the file exists.", _
' MsgBoxStyle.Exclamation, "Invalid Path or Filename")
'Catch all other errors. And delete partial files.
' Catch
fsInput.Close()
fsOutput.Close()
If Direction = CryptoAction.ActionDecrypt Then
Dim fileDelete As New FileInfo(txtDestinationDecrypt.Text)
fileDelete.Delete()
pbStatus.Value = 0
txtPassDecrypt.Text = ""
txtConPassDecrypt.Text = ""
MsgBox("Please check to make sure that you entered the correct" + _
"password.", MsgBoxStyle.Exclamation, "Invalid Password")
Else
Dim fileDelete As New FileInfo(txtDestinationEncrypt.Text)
fileDelete.Delete()
pbStatus.Value = 0
txtPassEncrypt.Text = ""
txtConPassEncrypt.Text = ""
MsgBox(Err.Description)
MsgBox("This file cannot be encrypted.", _
MsgBoxStyle.Exclamation, "Invalid File")
End If
' End Try
End Sub
Thank you very much!!!!
Last edited by a moderator: