Ok.. Now for me it does not work with encoding/ without encoding .. the code nextIn case when file NOT ENCRYPTED - the binary file stored on the disk correct.
When file is encrypted - I have a next :
Encrypting:
1) Dim contents As Byte() = File.ReadAllBytes(lcFilename) LENGHT is 85668
2) Dim encryptedContents As Byte() = loEncrypt.Encrypt(contents) - LENGHT is 85672
Decrypting:
1) Dim encryptedContents As Byte() = System.Text.Encoding.Unicode.GetBytes(contents) - LENGHT is 85672
2) loBytes = loEncrypt.Decrypt(encryptedContents) - LENGHT is 85671 !!!!!!!!!!!!!!!!
and as result - file not readable..
Below my code
1) Attach file to the database
If KontaktE_DOKUMENTEBO1.isembedded Then
Dim lcFilename As String = KontaktE_DOKUMENTEBO1.location
If File.Exists(lcFilename) Then
If KontaktE_DOKUMENTEBO1.geschuetzt = True Then ' When the document is password protected
Dim loEncrypt As MicroFour.StrataFrame.Security.Encryption.TripleDESWrapper = New MicroFour.StrataFrame.Security.Encryption.TripleDESWrapper
Dim contents As Byte() = File.ReadAllBytes(lcFilename)
Dim encryptedContents As Byte() = loEncrypt.Encrypt(contents)
Me.KontaktE_DOKUMENTEBO1.binarydata = System.Text.Encoding.Unicode.GetString(encryptedContents)
Else
Dim loBytes As Byte() = File.ReadAllBytes(lcFilename)
Me.KontaktE_DOKUMENTEBO1.binarydata = System.Text.Encoding.Unicode.GetString(loBytes)
End If
Try
File.Delete(lcFilename)
Catch ex As Exception
Dim lcMessage = ex.Message
End Try
End If
End If
2) Restore File to the Disk Code
Dim contents As String = Me.binarydata 'BusinessObject VarChar(MAX) field
Dim loBytes As Byte()
' When file neccessary to decrypt - then
If tlDecrypt Then
Dim loEncrypt As MicroFour.StrataFrame.Security.Encryption.TripleDESWrapper = New MicroFour.StrataFrame.Security.Encryption.TripleDESWrapper
Dim encryptedContents As Byte() = System.Text.Encoding.Unicode.GetBytes(contents)
loBytes = loEncrypt.Decrypt(encryptedContents)
Else
' When file not encrypted
loBytes = System.Text.Encoding.Unicode.GetBytes(contents)
End If
Dim fs As FileStream = New FileStream(tcFilename, FileMode.CreateNew, FileAccess.Write)
Dim bw As BinaryWriter = New BinaryWriter(fs)
bw.Write(loBytes, 0, loBytes.Length)
bw.Flush()
bw.Close()
fs.Close()