CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2009
    Posts
    100

    [RESOLVED] Data File Encryption

    Ok, so this may seem like a simple thread, but I haven't been able to find the answer. So I'm asking the community at large to see if I can get somewhere with this.

    I am writing an application that will read in several text files at various times, which is not an issue. The application, when finished, will be distributed to various users - along with those text files. What I need is to encrypt the text files so that they cannot be viewed or edited by end users once the end user has the application. You know, kind of like game data files that you get when you install a new game on your machine.

    Now, I was able to find out how to encrypt a text file, but I'm not sure if this is the correct answer. All things I've read on this indicate that once you encrypt it, it cannot be read by anyone on any other machine, including any applications that call them. Which is not what I want. I want the files encrypted, but only the application I'm writing to be able to read them.

    Is there a way to do this? Does VB have decryption routines that can be called to decrypt and read the files at runtime, and then re-encrypt them when it is done with the file? If so, can someone point me to a tutorial on how to do this so I can read up on the topic? If not, can someone suggest how to accomplish this?

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Data File Encryption

    There are several ways you could encrypt and decrypt a file that would work on any machine. For example a simple thing such as shifting the character a few places in either direction
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Data File Encryption

    I suggest you look up Encryption techniques, and then include the Encrpyt method in your file write sub, and the Decrypt in the file read sub..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Data File Encryption

    Don't know if my stupid suggestion would help. But what I have done once, to override this issue was to save the file with an unknown extension. This reduced the file being viewed dramatically because normal users tend to leave files with strange extensions alone.

    But, encryption is always better for security.
    I do have an applciation ( in .NET though ) that uses Rijndael as an encryption method, and it has been working good

  5. #5
    Join Date
    Aug 2009
    Posts
    100

    Re: Data File Encryption

    Quote Originally Posted by GremlinSA View Post
    I suggest you look up Encryption techniques, and then include the Encrpyt method in your file write sub, and the Decrypt in the file read sub..
    The files will be read-only, so I don't have to worry about encrypting during a write routine. I just need them encrypted to start with, and then decrypted when being read.

    EDIT: I attempted to do a google search to find a method or tutorial to decrypt a file using file i/o (read) in VB6, but I cannot find anything. Does anyone know of a tutorial they can point me to that explains how to accomplish decrypting files during program execution?
    Last edited by Leon Kennedy; June 7th, 2012 at 09:16 AM.

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Data File Encryption

    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Aug 2009
    Posts
    100

    Re: Data File Encryption

    So, I actually have access to a computer that has UltraEdit on it, and I was able to take the text files and encode them as Base64. I then saved them with a custom extension, so if someone were to try and open them they'd get the little "What program do you want to open this with" message. While this isn't the most secure, it's better than nothing. But now I've got another question on this that I can't find the answer to.

    Encoding in Base64 saves the formatting of the text, and when decoded (yes, I already have a routine that decodes Base64) it structures it exactly how it was when you encoded it. No problems there, other than text files don't have a whole lot of formatting options (such as bullets, indention, etc.). So I'm looking at trying to do this with Word documents, but I can't seem to find the answer.

    I have found an online site that I can type my text in to and have it converted, but again, no special formatting like you can do with Word documents. I could also just copy the base64 converted text into word and save it, but that's the same as having the text file. And if it's the same as the text file, then what's the point, right?

    So, does anybody know of a way to encode Word documents - formatting and all - as Base64? I looked through help files and google results, and I can't seem to find anything. If it isn't possible to encode in Base64, does anybody know of a way to encode Word documents in other formats, such as binary or hex?

  8. #8
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Data File Encryption

    Did you look at the links? If you use a good encrytion/decryption routine it will work with any kind of file.
    Always use [code][/code] tags when posting code.

  9. #9
    Join Date
    Aug 2009
    Posts
    100

    Re: Data File Encryption

    I looked at the links, but that's not what I'm after. The instructions given there are to encrypt and decrypt runtime, which is not what I want. While I have to decode/decrypt during runtime (when the file is accessed), I'd rather not have to use one routine to encrypt the file and then another to decrypt it.

    UltraEdit, if you are not familiar with it, is a text editor. I can type whatever I want in there, click Edit-Select All, and then click Edit-Encode Base64. It will, natively, encode the text as base64. At this point I can then save the document with whatever name and/or extension I want - the file is already encoded at this point.

    What UltraEdit does is what I'm hoping to find for Word - if there is a way to encode text natively so it can be encoded as formatted text with bullets and stuff. I don't want to have to create the word document, save it, then run a routine to encode it (although that isn't the worst idea in the world, I'd rather just encode it natively if I can).

    Can that be done? If not, then I'll either have to stick with UltraEdit OR use one of the links above. But I'd like to know if this is possible first.

  10. #10
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Data File Encryption

    I have Ultra Edit and use it quite a lot but I don't understand where you are coming from here. If you want the file to be encrypted then you have to encrypt/encode it using something. You then have to decrypt/decode it using something else.

    I can't see where it would matter if the encryption is done by your code [perhaps a different program just for that] or by Ultra Edit. So either way you are using one routine to encrypt and another to decrypt. The difference being that a good encryption routine would work with any kind of file and would be harder to crack.

    As for the word document, they already look very cryptic if you try to open them with notepad, just changing the extension to something that looks like it is not for word may suit your needs.
    Always use [code][/code] tags when posting code.

  11. #11
    Join Date
    Aug 2009
    Posts
    100

    Re: Data File Encryption

    After thinking on this for a while, I did one last google search to see if I could come up with something. And I found what I think is going to work.

    I came across a site called OpinionatedGeek.com, and they have an online utility that will take any entered text OR uploaded file and convert it to base64. So I typed some formatted text in Word, copied and pasted into the text box on the site, and converted it. I then saved the base64 encoding as a text file, and then ran it through some VB code to decode and view. Perfect!

    It's going to take a little more effort to create the text files than I'd like, but it will be worth it in the end.

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

Featured