Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > File Encryption


Reply
 
Thread Tools Display Modes
  #1  
Old 09-30-2009, 07:25 PM
ericponce ericponce is offline
Freshman
 
Join Date: Sep 2009
Posts: 36
Unhappy File Encryption



I am making a simple program that encrpts a text file and then decrpts it:
Can you please teel me why i get an error when i use this

Public Class Form1



Code:
 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        System.IO.File.Encrypt(TextBox1.Text)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        System.IO.File.Decrypt(TextBox1.Text)
    End Sub
End Class
Reply With Quote
  #2  
Old 09-30-2009, 08:05 PM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
Default

Is it safe for me to assume that Encrypt and Decrypt are existing functions?

What is the error that you get?
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #3  
Old 09-30-2009, 08:48 PM
ericponce ericponce is offline
Freshman
 
Join Date: Sep 2009
Posts: 36
Default

they are existing i get this exact error
Code:
System.IO.IOException was unhandled
  Message="The request is not supported. "
Reply With Quote
  #4  
Old 09-30-2009, 10:14 PM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

Take a look at the documentation, particularly the exceptions section:

Quote:
IOException

An I/O error occurred while opening the file.

-or-

This operation is not supported on the current platform.
Obviously it's not supported on your platform. My guess is you're not running XP or greater with NTFS as the drive formatting.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #5  
Old 10-01-2009, 04:18 AM
ericponce ericponce is offline
Freshman
 
Join Date: Sep 2009
Posts: 36
Default

i am. i just dont know what im ding wrong there has to be something
Reply With Quote
  #6  
Old 10-01-2009, 08:06 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

It's not that your code is trying to do anything wrong. The exception is being thrown because the Encrypt() method doesn't work on all platforms and it's being called on a platform that does not support it. If this is the case, it's likely there's nothing you can do to make it work other than change versions of your operating system.

Are you using Windows XP Home edition? this article says that EFS isn't supported in that. I'd also inspect any settings related to encryption. For example, create a test folder from Windows Explorer and enable encryption in that folder. Can you encrypt/decrypt files in that folder?

If you feel like you've exhausted all options, I suggest filing a bug at http://connect.microsoft.com . If you do so, give them much more detail than you've given us or they'll never address it.

Have you considered using classes from the System.Security.Cryptography namespace to manually encrypt the file? It won't be as easy, but it's much more likely to be supported on your machine.

Last edited by AtmaWeapon; 10-01-2009 at 04:23 PM.
Reply With Quote
  #7  
Old 10-01-2009, 03:50 PM
ericponce ericponce is offline
Freshman
 
Join Date: Sep 2009
Posts: 36
Default

Im on Vista and it should work why would microsoft make a command that dosent work on there latest system. There must be something else thats wrong
Reply With Quote
  #8  
Old 10-01-2009, 04:35 PM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

Hmm, doesn't work for me either on Windows 7 64-bit. My guess is there's some system setting that needs to be turned on, but I can't seem to find anything about it.
Reply With Quote
  #9  
Old 10-01-2009, 04:47 PM
hawkvalley1's Avatar
hawkvalley1 hawkvalley1 is offline
Centurion
 
Join Date: May 2008
Location: Denver, CO USA
Posts: 190
Default

Would something like this be helpful?
Code:
Private DES As New TripleDESCryptoServiceProvider
Private MD5 As New MD5CryptoServiceProvider

Public Function MD5Hash(ByVal value As String) As Byte()
    Return MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(value))
End Function

Public Function Encrypt(ByVal stringToEncrypt As String, ByVal key As String) As String
    DES.Key = MD5Hash(key)
    DES.Mode = CipherMode.ECB
    Dim Buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(stringToEncrypt)
    Return Convert.ToBase64String(DES.CreateEncryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
End Function

Public Function Decrypt(ByVal encryptedString As String, ByVal key As String) As String
    Try  
    DES.Key = MD5Hash(key)
    DES.Mode = CipherMode.ECB
    Dim Buffer As Byte() = Convert.FromBase64String(encryptedString)
    Return ASCIIEncoding.ASCII.GetString(DES.CreateDecryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
   Catch ex As Exception
         MessageBox.Show("Wrong Key Number, decryption not available!")
   End Try 
End Function
Just encrypt the string before you write to the stream - remember the key must be the same both ways.
Reply With Quote
  #10  
Old 10-01-2009, 07:15 PM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

ECB is the least secure mode for encryption. As Atwood's post points out, there's a million examples of people using it on the internet; it's like a virus how it propagates.
Reply With Quote
  #11  
Old 10-01-2009, 08:03 PM
ericponce ericponce is offline
Freshman
 
Join Date: Sep 2009
Posts: 36
Default

it is great i just dont understand it at all..... But thanks anyways and it dosent matter to me if its not secure i just want to try to make it a simple as possible and want to learn what encrpyt decrypt does it must have some use.
Reply With Quote
  #12  
Old 10-01-2009, 08:05 PM
hawkvalley1's Avatar
hawkvalley1 hawkvalley1 is offline
Centurion
 
Join Date: May 2008
Location: Denver, CO USA
Posts: 190
Default

Ok, good to know, I haven't used this in some time, so what is the newest, shiniest, encryption now?
Reply With Quote
  #13  
Old 10-01-2009, 08:29 PM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

I have no idea because I didn't pay much attention in my cryptography class. The article I linked to recommends CBC, but other people also point out that 3DES may be inferior to algorithms like AES.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->