I used this function to encrypt and decrypt any files in my aplication but i've got problem.
When I use this on .jpg, .png etc. (pictures) everything is allright but if i need to encode and decode .txt or .doc/.docx Ive got problem like:
when I put orginal .txt like:
SELECT TOP 1 @id = id, @active = active_b FROM active_b _replacement
I obtain after decryptionthis:
SELECT TOP 1 @id = id, @active = active_b FROM as˝˝lA)ć?????c
I have no idea why. Is there any unrecognised characters to this algorithm?
I can't use this for .doc, .docx too.
Any ideas, suggestions, tips?
Used Function:
When I use this on .jpg, .png etc. (pictures) everything is allright but if i need to encode and decode .txt or .doc/.docx Ive got problem like:
when I put orginal .txt like:
SELECT TOP 1 @id = id, @active = active_b FROM active_b _replacement
I obtain after decryptionthis:
SELECT TOP 1 @id = id, @active = active_b FROM as˝˝lA)ć?????c
I have no idea why. Is there any unrecognised characters to this algorithm?
I can't use this for .doc, .docx too.
Any ideas, suggestions, tips?
Used Function:
HTML:
Public Function EncryptOrDecryptFile(ByVal strInputFile As String, _
ByVal strOutputFile As String, _
ByVal Key As String, _
ByVal Direction As Integer)
Try 'In case of errors.
'Setup file streams to handle input and output.
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
'Determine if ecryption or decryption and setup CryptoStream.
Select Case Direction
Case 1
csCryptoStream = New CryptoStream(fsOutput, _
cspRijndael.CreateEncryptor(CreateKey(Key), CreateIV(Key)), _
CryptoStreamMode.Write)
Case 2
csCryptoStream = New CryptoStream(fsOutput, _
cspRijndael.CreateDecryptor(CreateKey(Key), CreateIV(Key)), _
CryptoStreamMode.Write)
End Select
'Use While to loop until all of the file is processed.
While lngBytesProcessed < lngFileLength
'Read file with the input filestream.
intBytesInCurrentBlock = fsInput.Read(bytBuffer, 0, 4096)
'Write output file with the cryptostream.
csCryptoStream.Write(bytBuffer, 0, intBytesInCurrentBlock)
'Update lngBytesProcessed
lngBytesProcessed = lngBytesProcessed + _
CLng(intBytesInCurrentBlock)
End While
fsInput.Close()
'Close FileStreams and CryptoStream.
csCryptoStream.Close()
fsOutput.Close()
Catch ex As Exception
Try
fsInput.Close()
Catch
End Try
Try
fsOutput.Close()
Catch
End Try
End Try
End Function