protect file

First Mandc 1 Reputation point
2022-03-29T16:08:29.307+00:00

hi
I have a database in which to save the PDF files.
I save the files and open the file after moving it to the folder of the program path and running it inside my program. All this is fine without problems.
What I want is when writing the file with the path of the program while the program is working, not to copy or open it except through a program.
The method that I have now is ineffective to hide the file.
But if the program is working with the file, the file will be copied without a problem. I want to protect the file.
Or if it is possible to open the file without writing it in a folder on the device and open it directly from the database.

my code work

con.Open()
                    Dim sql As String = "SELECT BOOK_ID,BOOK_FILE FROM  BOOK_TB   WHERE BOOK_ID=" & "2" & "  ORDER BY BOOK_ID DESC   LIMIT 1"
                    Dim cmd As SQLiteCommand = New SQLiteCommand(sql, con)
                    Dim dr As SQLiteDataReader = cmd.ExecuteReader()
                    Dim data As Byte() = Nothing

                    While dr.Read()
                        data = CType(dr(1), Byte())
                    End While

                    Using fs = New FileStream(Path.Combine("E:\", "file.PDF"), FileMode.Create, FileAccess.Write)
                        fs.Write(data, 0, data.Length)
                    End Using
                    con.Close()
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,714 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. LesHay 7,126 Reputation points
    2022-03-29T17:26:21.383+00:00

    Hi

    One way would be to Encrypt the file, then Decrypt on use.
    Here is a code Module (little exception handling) which I think I got from Microsoft sometime ago to Encrypt and Decrypt files.

    If you want to go this route then I can give the way to use the Module (actually very simple).
    The Decrypted file could be loaded into a control on a Form, or saved to a new file - whatever is needed.

    Option Strict On
    Option Explicit On
    Option Infer Off
    Imports System.IO
    Imports System.Runtime.InteropServices
    Imports System.Security.Cryptography
    Imports System.Text
    
    Module Module1
      ' Call this function to remove the key from memory after it is used for security.
      <DllImport("kernel32.dll")>
      Public Sub ZeroMemory(ByVal addr As IntPtr, ByVal size As Integer)
      End Sub
    
      Sub MyEncryptFile(fn As String, fn2 As String, key As String)
        ' Encrypt the file.        
        EncryptFile(fn, fn2, key)
      End Sub
      Sub MyDecryptFile(fn As String, fn2 As String, key As String)
        ' Encrypt the file.        
        DecryptFile(fn, fn2, key)
      End Sub
    
      ' Function to generate a 64-bit key.
      Function GenerateKey() As String
        ' Create an instance of a symmetric algorithm. The key and the IV are generated automatically.
        Dim desCrypto As DES = DES.Create()
    
        ' Use the automatically generated key for encryption. 
        Return ASCIIEncoding.ASCII.GetString(desCrypto.Key)
    
      End Function
    
      Sub EncryptFile(ByVal sInputFilename As String,
                     ByVal sOutputFilename As String,
                     ByVal sKey As String)
    
        Dim fsInput As New FileStream(sInputFilename,
                                    FileMode.Open, FileAccess.Read)
        Dim fsEncrypted As New FileStream(sOutputFilename,
                                    FileMode.Create, FileAccess.Write)
    
        Dim DES As New DESCryptoServiceProvider()
    
        'Set secret key for DES algorithm.
        'A 64-bit key and an IV are required for this provider.
        DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
    
        'Set the initialization vector.
        DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
    
        'Create the DES encryptor from this instance.
        Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
        'Create the crypto stream that transforms the file stream by using DES encryption.
        Dim cryptostream As New CryptoStream(fsEncrypted,
                                            desencrypt,
                                            CryptoStreamMode.Write)
    
        'Read the file text to the byte array.
        Dim bytearrayinput(CInt(fsInput.Length - 1)) As Byte
        fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
        'Write out the DES encrypted file.
        cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
        cryptostream.Close()
        fsInput.Close()
        fsEncrypted.Close()
      End Sub
    
      Sub DecryptFile(ByVal sInputFilename As String,
          ByVal sOutputFilename As String,
          ByVal sKey As String)
    
        Dim DES As New DESCryptoServiceProvider()
        'A 64-bit key and an IV are required for this provider.
        'Set the secret key for the DES algorithm.
        DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
        'Set the initialization vector.
        DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
    
        'Create the file stream to read the encrypted file back.
        Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
        'Create the DES decryptor from the DES instance.
        Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
        'Create the crypto stream set to read and to do a DES decryption transform on incoming bytes.
        Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
        'Print out the contents of the decrypted file.
        Dim fsDecrypted As New StreamWriter(sOutputFilename)
        fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
        fsDecrypted.Flush()
        fsDecrypted.Close()
        fsread.Close()
      End Sub
    End Module
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.