Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB.net open File with new NTFS Permissions / open PDF wtih damaged Header

Posted on 2013-06-11
8
Medium Priority
?
1,237 Views
Last Modified: 2013-06-13
Hello I try to create a read only Solution for a PDF document.
I like to deny the potability of copying this document.

My Idea is to store the Document on a Network drive with no access.
Next step will be a VB Program with a list of all files and then access the PDF File with the internal PDF Viewer in VB.net. This Viewer should then be programmed to not allow Copy Paste or Save in other directions.

Other solution is to destroy the header of the PDF file so youre not able to open it with normal PDF Adobe reader only with this VB Program that should then be able to change the header back before opening.

Any Ideas?
0
Comment
Question by:HelpdeskJBC
  • 4
  • 4
8 Comments
 
LVL 7

Expert Comment

by:HaiFai
ID: 39237308
What is the question ?
maybe its better to crypt file from vb.net and store it in crypted format and decrypt when you open it from vb, if that was the question
http://support.microsoft.com/kb/301070
0
 

Author Comment

by:HelpdeskJBC
ID: 39237357
Question in Detail ist "best practice"
and if some one would be nice and could start a project with these options for me in Visual Studio (I'm using 2010)

encrypt and decrypt also sounds create but is the file accessible after decrypt? we want to forbid copy and save as options to the file.

I know its technically not possible because if I open a file its automatically in the RAM but my options above should make it much more complicated to use the file outside the company or opening it with a different program...

So could anyone please start a project with those options?
I can do the error handling and fine tuning.
Thanks
0
 
LVL 7

Accepted Solution

by:
HaiFai earned 1500 total points
ID: 39240149
if you want to use adobe acrobat reader to read pdf in your project its simle
just add control to toolbar like this
rigth click toolbox -- choose items-- com components --- adobe pdf reader.
then just add control to your form.
in code you can open pdf file like this  AxAcroPDF1.src = c:\test.pdf

encrypt and decrypt also sounds create but is the file accessible after decrypt? we want to forbid copy and save as options to the file.
yes seems like adobe pdf reader cant read file from memory stream so you kind of need to decrypt file to temp folder in local computer and open it there, but its same thing with destroyed header file or use another component to open pdf.

but anyway you could start like this.

i was lazy and downloaded this cryptostuff.vb code from here
http://www.vb-helper.com/howto_net_crypt_file.html
cryptostuff.vb

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Diagnostics
Imports System.Runtime.CompilerServices

Module CryptoStuff

    ' Use the password to generate key bytes.
    Private Sub MakeKeyAndIV(ByVal password As String, ByVal salt() As Byte, ByVal key_size_bits As Integer, ByVal block_size_bits As Integer, ByRef key() As Byte, ByRef iv() As Byte)
        Dim derive_bytes As New Rfc2898DeriveBytes(password, salt, 1000)

        key = derive_bytes.GetBytes(key_size_bits / 8)
        iv = derive_bytes.GetBytes(block_size_bits / 8)
    End Sub

#Region "Encrypt Files and Streams"

    ' Encrypt or decrypt a file, saving the results in another file.
    Public Sub EncryptFile(ByVal password As String, ByVal in_file As String, ByVal out_file As String)
        CryptFile(password, in_file, out_file, True)
    End Sub
    Public Sub DecryptFile(ByVal password As String, ByVal in_file As String, ByVal out_file As String)
        CryptFile(password, in_file, out_file, False)
    End Sub
    Public Sub CryptFile(ByVal password As String, ByVal in_file As String, ByVal out_file As String, ByVal encrypt As Boolean)
        ' Create input and output file streams.
        Using in_stream As New FileStream(in_file, FileMode.Open, FileAccess.Read)
            Using out_stream As New FileStream(out_file, FileMode.Create, FileAccess.Write)
                ' Encrypt/decrypt the input stream into the output stream.
                CryptStream(password, in_stream, out_stream, encrypt)
            End Using
        End Using
    End Sub

    ' Encrypt the data in the input stream into the output stream.
    Public Sub CryptStream(ByVal password As String, ByVal in_stream As Stream, ByVal out_stream As Stream, ByVal encrypt As Boolean)
        ' Make an AES service provider.
        Dim aes_provider As New AesCryptoServiceProvider()

        ' Find a valid key size for this provider.
        Dim key_size_bits As Integer = 0
        For i As Integer = 1024 To 1 Step -1
            If (aes_provider.ValidKeySize(i)) Then
                key_size_bits = i
                Exit For
            End If
        Next i
        Debug.Assert(key_size_bits > 0)
        Console.WriteLine("Key size: " & key_size_bits)

        ' Get the block size for this provider.
        Dim block_size_bits As Integer = aes_provider.BlockSize

        ' Generate the key and initialization vector.
        Dim key() As Byte = Nothing
        Dim iv() As Byte = Nothing
        Dim salt() As Byte = {&H0, &H0, &H1, &H2, &H3, &H4, &H5, &H6, &HF1, &HF0, &HEE, &H21, &H22, &H45}
        MakeKeyAndIV(password, salt, key_size_bits, block_size_bits, key, iv)

        ' Make the encryptor or decryptor.
        Dim crypto_transform As ICryptoTransform
        If (encrypt) Then
            crypto_transform = aes_provider.CreateEncryptor(key, iv)
        Else
            crypto_transform = aes_provider.CreateDecryptor(key, iv)
        End If

        ' Attach a crypto stream to the output stream.
        ' Closing crypto_stream sometimes throws an
        ' exception if the decryption didn't work
        ' (e.g. if we use the wrong password).
        Try
            Using crypto_stream As New CryptoStream(out_stream, crypto_transform, CryptoStreamMode.Write)
                ' Encrypt or decrypt the file.
                Const block_size As Integer = 1024
                Dim buffer(block_size) As Byte
                Dim bytes_read As Integer
                Do
                    ' Read some bytes.
                    bytes_read = in_stream.Read(buffer, 0, block_size)
                    If (bytes_read = 0) Then Exit Do

                    ' Write the bytes into the CryptoStream.
                    crypto_stream.Write(buffer, 0, bytes_read)
                Loop
            End Using
        Catch
        End Try

        crypto_transform.Dispose()
    End Sub

#End Region ' Encrypt Files and Streams"

#Region "Encrypt Strings and Byte()"
    ' Note that extension methods must be defined in a non-generic static class.

    ' Encrypt or decrypt the data in in_bytes[] and return the result.
    Public Function CryptBytes(ByVal password As String, ByVal in_bytes() As Byte, ByVal encrypt As Boolean) As Byte()
        ' Make an AES service provider.
        Dim aes_provider As New AesCryptoServiceProvider()

        ' Find a valid key size for this provider.
        Dim key_size_bits As Integer = 0
        For i As Integer = 1024 To 1 Step -1
            If (aes_provider.ValidKeySize(i)) Then
                key_size_bits = i
                Exit For
            End If
        Next i
        Debug.Assert(key_size_bits > 0)
        Console.WriteLine("Key size: " & key_size_bits)

        ' Get the block size for this provider.
        Dim block_size_bits As Integer = aes_provider.BlockSize

        ' Generate the key and initialization vector.
        Dim key() As Byte = Nothing
        Dim iv() As Byte = Nothing
        Dim salt() As Byte = {&H0, &H0, &H1, &H2, &H3, &H4, &H5, &H6, &HF1, &HF0, &HEE, &H21, &H22, &H45}
        MakeKeyAndIV(password, salt, key_size_bits, block_size_bits, key, iv)

        ' Make the encryptor or decryptor.
        Dim crypto_transform As ICryptoTransform
        If (encrypt) Then
            crypto_transform = aes_provider.CreateEncryptor(key, iv)
        Else
            crypto_transform = aes_provider.CreateDecryptor(key, iv)
        End If

        ' Create the output stream.
        Using out_stream As New MemoryStream()
            ' Attach a crypto stream to the output stream.
            Using crypto_stream As New CryptoStream(out_stream, _
                    crypto_transform, CryptoStreamMode.Write)
                ' Write the bytes into the CryptoStream.
                crypto_stream.Write(in_bytes, 0, in_bytes.Length)
                Try
                    crypto_stream.FlushFinalBlock()
                Catch ex As CryptographicException
                    ' Ignore this exception. The password is bad.
                Catch
                    ' Re-throw this exception.
                    Throw
                End Try

                ' return the result.
                Return out_stream.ToArray()
            End Using
        End Using
    End Function

    ' String extensions to encrypt and decrypt strings.
    <Extension()> _
    Public Function Encrypt(ByVal the_string As String, ByVal password As String) As Byte()
        Dim ascii_encoder As New System.Text.ASCIIEncoding()
        Dim plain_bytes() As Byte = ascii_encoder.GetBytes(the_string)
        Return CryptBytes(password, plain_bytes, True)
    End Function
    <Extension()> _
    Public Function Decrypt(ByVal the_bytes() As Byte, ByVal password As String) As String
        Dim decrypted_bytes() As Byte = CryptBytes(password, the_bytes, False)
        Dim ascii_encoder As New System.Text.ASCIIEncoding()
        Return ascii_encoder.GetString(decrypted_bytes)
    End Function
    Public Function CryptString(ByVal password As String, ByVal in_string As String, ByVal encrypt As Boolean) As String
        ' Make a stream holding the input string.
        Dim in_bytes() As Byte = Encoding.ASCII.GetBytes(in_string)
        Using in_stream As New MemoryStream(in_bytes)
            ' Make an output stream.
            Using out_stream As New MemoryStream()
                ' Encrypt.
                CryptStream(password, in_stream, out_stream, True)

                ' Return the result.
                out_stream.Seek(0, SeekOrigin.Begin)
                Using stream_reader As New StreamReader(out_stream)
                    Return stream_reader.ReadToEnd()
                End Using
            End Using
        End Using
    End Function

    ' Convert a byte array into a readable string of hexadecimal values.
    <Extension()> _
    Public Function ToHex(ByVal the_bytes() As Byte) As String
        Return ToHex(the_bytes, False)
    End Function
    <Extension()> _
    Public Function ToHex(ByVal the_bytes() As Byte, ByVal add_spaces As Boolean) As String
        Dim result As String = ""
        Dim separator As String = ""
        If (add_spaces) Then separator = " "
        For i As Integer = 0 To the_bytes.Length - 1
            result &= the_bytes(i).ToString("x2") & separator
        Next i
        Return result
    End Function

    ' Convert a string containing 2-digit hexadecimal values into a byte array.
    <Extension()> _
    Public Function ToBytes(ByVal the_string As String) As Byte()
        Dim the_bytes As New List(Of Byte)()
        the_string = the_string.Replace(" ", "")

        For i As Integer = 0 To the_string.Length - 1 Step 2
            the_bytes.Add( _
                Byte.Parse(the_string.Substring(i, 2), _
                    System.Globalization.NumberStyles.HexNumber))
        Next i
        Return the_bytes.ToArray()
    End Function

#End Region ' Encrypt Strings and Byte()

End Module

Open in new window



so include that in project allso add that adobe pdf reader component
then 2 buttons and openfiledialog

in button 1 you can crypt file in button 2 you can decrypt and open pdf to form.
Public Class Form1
    Dim pass As String = "password"

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.ShowDialog()
        CryptoStuff.EncryptFile(pass, OpenFileDialog1.FileName, OpenFileDialog1.FileName & ".crypted")
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        OpenFileDialog1.ShowDialog()
        CryptoStuff.DecryptFile(pass, OpenFileDialog1.FileName, "c:\" & OpenFileDialog1.SafeFileName)
        AxAcroPDF1.src = "c:\" & OpenFileDialog1.SafeFileName

    End Sub
End Class

Open in new window



allso you may want to check all options for pdf how you can customize control
http://partners.adobe.com/public/developer/en/acrobat/sdk/pdf/iac/IACReference.pdf

few examples
AxAcroPDF1.setShowToolbar(False)
AxAcroPDF1.setView("fitH")
AxAcroPDF1.setLayoutMode("SinglePage")
AxAcroPDF1.setShowScrollbars(False)
AxAcroPDF1.setPageMode("none")
so user cannot save page from pdf reader so easy
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:HelpdeskJBC
ID: 39240488
Thanks for the code looks good!
Although this line isn't working AxAcroPDF1.setShowToolbar(False)
I still see the gray rectangle with Save and Print option on the screen.

Also I'm still thinking of the Temp File Problem.
Is it possible to use a hidden Shared Folder?
Better Solution is it possible to save and open Files from a Network path where the User normally has no access? So during vb.net access use a different hard-coded Account for accessing this special Folder?
0
 
LVL 7

Expert Comment

by:HaiFai
ID: 39240542
Is it possible to use a hidden Shared Folder?
Yes its possible to use shared folder, but user need to have write rigths to folder.

Better Solution is it possible to save and open Files from a Network path where the User normally has no access? So during vb.net access use a different hard-coded Account for accessing this special Folder?
This is working too but you need to impresonate rights in your code to allow save file to folder
allso you cannot use same server where user is allready logged it with own account (this is restriction from windows)
0
 

Author Comment

by:HelpdeskJBC
ID: 39240549
Ok the hidden temp folder is working only one problem left:
I'm not able to hide the toolbar this Small grey thing always comes up with this Small menu save print etc.
Any ideas?
0
 

Author Closing Comment

by:HelpdeskJBC
ID: 39243652
Still need to know how to disable floating toolbar
0
 
LVL 7

Expert Comment

by:HaiFai
ID: 39243920
hmm yes this seems like it does not work correcly AxAcroPDF1.setShowToolbar(False)
maybe its bug or something, according guide it should work like that.

i checked #toolbar=0&navpanes=0 it seems to work in webpage, so maybe if you try this

add WebBrowser1 component to project and load pdf like this
 Dim fileurl As New Uri(OpenFileDialog1.FileName & "#toolbar=0&navpanes=0")
WebBrowser1.Navigate(fileurl)

Open in new window

its using webbrowser then to access pdf and disable panel like it should do in AxAcroPDF1
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a previous article published here at Experts Exchange, Signature Image with Transparent Background (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_12380-Signature-Image-with-Transparent-Background.html), I explained how to cre…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Sometimes we receive PDF files that are in the wrong orientation. They may be sideways or even upside down. This most commonly happens with scanned or faxed documents. It is possible to rotate the view of these PDFs with the free Adobe Reader produc…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question