Results 1 to 5 of 5

Thread: What does File.Encrypt do?

  1. #1

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    What does File.Encrypt do?

    It seems I cannot use File.Encrypt when creating a text file. And if I am supposed to use something else, why is File.Encrypt there?
    As usual, thanks for the help in advance.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: What does File.Encrypt do?

    You can't use File.Encrypt when creating any file. That method operates on a file in place. You would create the file exactly as you normally would and then call File.Encrypt to encrypt it in place. When you want to read the file, you call File.Decrypt to decrypt it in place and then read it as you usually would.

    Note that the point of those methods is that only the Windows user account that executed the Encrypt method can decrypt the file by using the Decrypt method. Even if it's the same application, decryption won't work if it's running under a different account that performed the encryption. Also, other applications can decrypt the file simply by calling Decrypt, as long as they are running under the same user account as did the encryption.

    If that's not what you want then you can use functionality from the System.IO.Cryptography namespace, most importantly the CryptoStream class. For instance, normally a StreamReader or StreamWriter would sit on top of a FileStream to read and write text files. You can have them sit on top of a CryptoStream instead and then have that sit on top of a FileStream so that your application reads and writes text from and to an encrypted file. In that case, you specify the encryption algorithm and key so there's no specific dependency on the Windows user account.

  3. #3

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: What does File.Encrypt do?

    Well in that case I don't understand why this bit code doesn't work:
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim SW As StreamWriter = New StreamWriter("C:\EncryptionTest.txt")
            SW.WriteLine(TextBox1.Text)
            SW.Dispose
            File.Encrypt("C:\EncryptionTest.txt")
    End Sub
    Even File.Encrypt("C:\EncryptionTest.txt") by itself gives me the error.
    Edit: I forgot to add SW.Dispose so I did but I still get the error.
    Last edited by NinjaNic; Apr 22nd, 2014 at 10:11 PM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: What does File.Encrypt do?

    Quote Originally Posted by NinjaNic View Post
    Well in that case I don't understand why this bit code doesn't work:
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim SW As StreamWriter = New StreamWriter("C:\EncryptionTest.txt")
            SW.WriteLine(TextBox1.Text)
            SW.Dispose
            File.Encrypt("C:\EncryptionTest.txt")
    End Sub
    Even File.Encrypt("C:\EncryptionTest.txt") by itself gives me the error.
    Edit: I forgot to add SW.Dispose so I did but I still get the error.
    If only there was some way for us to know what the error message was. Have you read the documentation for the File.Encrypt method to see what exceptions it can throw and, if one of them matches the one you're getting, what the possible causes are? It only takes a couple of seconds to press F1 or use the Help menu. That's exactly what I did to provide the information I did in my previous post because I didn't even know that there was a File.Encrypt method before that.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: What does File.Encrypt do?

    I just tested for myself and I got this error message:
    Recovery policy configured for this system contains invalid recovery certificate.
    From the very quick research that I did, it seems that you need to set up a certificate first.

    https://www.google.com.au/search?q=R...CMHC8gebkIHABw

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width