CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Nov 2010
    Posts
    139

    how can i hide an encrypted file?

    hi, i would like to hide a .txt file that my program has encrypted, i was just going to leave it on the system once encrypted (without hiding it) but then i realised that it can be tampered with, in that the contents can be deleted if someone opens the encrypted file using windows explorer. is there some sort of function that can hide the file after it has been encrypted?
    Last edited by katy_price; January 4th, 2011 at 12:18 AM.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: how can i hide an encrypted file?

    I don't know of any better Windows-only-based way to do that. There may be ways to do that using 3rd-party tools, however.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Nov 2010
    Posts
    139

    Re: how can i hide an encrypted file?

    do you think its possible to make the file read only if its opened via windows explorer but editable if its opened up through my program?

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: how can i hide an encrypted file?

    Unfortunately, the read-only attribute is as weak as the hidden attribute.

    You probably can, however, achieve something roughly equivalent if your app runs under a different user account from that of the users, and your app has full access permissions to that file while those of the user are restricted. I have no experience with using tricks like that, though.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Nov 2010
    Posts
    139

    Re: how can i hide an encrypted file?

    if i do that then the file can still be tampered with if opened via windows explorer

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: how can i hide an encrypted file?

    Quote Originally Posted by katy_price View Post
    if i do that then the file can still be tampered with if opened via windows explorer
    If the "that" in this sentence means the access permissions trick, then no, it can't.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  7. #7
    Join Date
    Nov 2010
    Posts
    139

    Re: how can i hide an encrypted file?

    Quote Originally Posted by Eri523 View Post
    Unfortunately, the read-only attribute is as weak as the hidden attribute.
    how do i do this read-only thing? it don't matter if its weak.

  8. #8
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: how can i hide an encrypted file?

    Quote Originally Posted by katy_price View Post
    how do i do this read-only thing? it don't matter if its weak.
    In one of the other threads, when it was about checking for file modifications using the archive attribute, I already pointed you to the System::IO::File class and its GetAttributes() and SetAttributes() members. You would use them for this purpose as well, just manipulating the FileAttributes::ReadOnly now.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  9. #9
    Join Date
    Nov 2010
    Posts
    139

    Re: how can i hide an encrypted file?

    Quote Originally Posted by Eri523 View Post
    In one of the other threads, when it was about checking for file modifications using the archive attribute, I already pointed you to the System::IO::File class and its GetAttributes() and SetAttributes() members. You would use them for this purpose as well, just manipulating the FileAttributes::ReadOnly now.
    how do i code that for the encrypt file, i have placed FileAttribute::ReadOnly after the file is encrypted but it don't work. is there meant to be a link between the readonly and the file name? if so how?

  10. #10
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: how can i hide an encrypted file?

    Quote Originally Posted by katy_price View Post
    [...] i have placed FileAttribute::ReadOnly after the file is encrypted but it don't work.
    You mean you put it on a line by its own? As FileAttributes::ReadOnly is a constant this simply does nothing.

    is there meant to be a link between the readonly and the file name?
    Of course there is a relation between the two. That's because each file has its own set of attributes.

    if so how?
    FileAttributes is a set of bit flags represented by this enum type. (The fact that it consists of bit flags is denoted by [FlagsAttribue] in the C++/CLI declaration of FileAttributes shown in the MSDN docs.) Each one of the bit flags represents one of the attributes which can either be turned on or off.

    Each one of the following samples assumes that you have a

    Code:
    using namespace System::IO;
    at the beginning of your code.

    Manipulating bit flags is some old-fashioned low-level-like stuff. You manipulate them using binary (i.e. bit-wise) operations. The sequece of actions is to read the attributes, manipulate the flag(s) you want and finally write the attributes back.

    Setting the read-only attribute looks like this:

    Code:
    FileAttributes fa;
    fa = File::GetAttributes("YourFile.dat");
    fa = fa | FileAttributes::ReadOnly;
    File::SetAttributes("YourFile.dat", fa);
    Resetting it looks like this:

    Code:
    FileAttributes fa;
    fa = File::GetAttributes("YourFile.dat");
    fa = fa & ~FileAttributes::ReadOnly;
    File::SetAttributes("YourFile.dat", fa);
    This bit-wise mumbo-jumbo is required to only modify the flag you want while keeping the others.

    And of course YourFile.dat is a sample file name, and in particular the .dat is just a sample file extension, not some sort of extra magic.

    Setting the read-only attribute (e.g.) can be contracted to:

    Code:
    File::SetAttributes("YourFile.dat", File::GetAttributes("YourFile.dat") | FileAttributes::ReadOnly);
    This is less obvious but eliminates the need for the temporary fa variable.

    Some of the attributes can also be inspected or manipulated in the file's properties dialog in Windows Explorer, BTW.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  11. #11
    Join Date
    Nov 2010
    Posts
    139

    Re: how can i hide an encrypted file?

    this doesn't work the file can encrypted file can still be edited when opened from windows explorer.

    Code:
    FileAttributes fa;
    fa = File::GetAttributes("YourFile.dat");
    fa = fa | FileAttributes::ReadOnly;
    File::SetAttributes("YourFile.dat", fa);

  12. #12
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: how can i hide an encrypted file?

    You can open and modify the file, but you can't save the modified data back to the same file. That's how the read-only attribute works.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  13. #13
    Join Date
    Nov 2010
    Posts
    139

    Re: how can i hide an encrypted file?

    omg thanks thats brilliant i just tried to save it and it wouldn't....thanks once again

  14. #14
    Join Date
    Nov 2010
    Posts
    139

    Re: how can i hide an encrypted file?

    sorry i just thought someone can also delete this file through explorer, is there a way of preventing deletion too?

  15. #15
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: how can i hide an encrypted file?

    Sorry, if the user tries to delete the file using Windows Explorer it will just issue a warning. If he/she clicks on "yes, delete it" it'll proceed.

    There's no way around this with plain file attributes. Everything else would be much more complicated.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Page 1 of 2 12 LastLast

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