By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,665 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,665 IT Pros & Developers. It's quick & easy.

How to create password protected files thru C#

P: n/a
Hi,

I want to create a log file from my C# code. Currently I am encrypting
the contents of this log file, but now the requirement is that no one
should not be able to open this file from Windows Explorer. (Currently
if we open the file using some editor, the encrypted contents are
displayed)

The file should only be read/modified from within the program.

Is it possible to achieve this functionality in C#? Can i apply some
password to this file from within my program?

Any help in this regard is greatly appreciated.
Regards,
Ankit!!
Dec 11 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Ankit,

Not really. You can't set permissions on a file on an application
basis. Users and groups have rights which are used to resolve access to the
file.

Now, you can encrypt the file, and then make it so only your app has
access to the encryption key (for decrypting the file).

You could also create a shell extension for explorer in an unmanaged
language (you shouldn't create them in .NET) which would prevent other
programs from opening the file (I think), but honestly, I think that is the
wrong approach.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<aa*******@gmail.comwrote in message
news:77**********************************@s19g2000 prg.googlegroups.com...
Hi,

I want to create a log file from my C# code. Currently I am encrypting
the contents of this log file, but now the requirement is that no one
should not be able to open this file from Windows Explorer. (Currently
if we open the file using some editor, the encrypted contents are
displayed)

The file should only be read/modified from within the program.

Is it possible to achieve this functionality in C#? Can i apply some
password to this file from within my program?

Any help in this regard is greatly appreciated.
Regards,
Ankit!!

Dec 11 '07 #2

P: n/a
Another option is to move the file from the filing system into a database
clob field; the application can reverse the process (temporarily) when it
wants to access the file content.

Dec 11 '07 #3

P: n/a
If the contents of the log file are already encrypted, what is the purpose of
enforcing some policy that says nobody is allowed to load the file from the
filesystem? All they would ever see is encrypted characters. If you really
don't want people to even be able to "see" the file, then storing it in the
database as mentioned already would be a good choice. You can encrypta and
decrypt right from within SQL Server:

DECLARE @cleartext NVARCHAR(100)
DECLARE @encryptedstuff NVARCHAR(100)
DECLARE @decryptedstuff NVARCHAR(100)
SET @cleartext = 'This is text that needs to be encrypted.'
SET @encryptedstuff = EncryptByPassPhrase('pigscanfly', @cleartext)
SELECT @encryptedstuff
SET @decryptedstuff = DecryptByPassphrase('pigscanfly', @encryptedstuff)
SELECT @decryptedstuff
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
"aa*******@gmail.com" wrote:
Hi,

I want to create a log file from my C# code. Currently I am encrypting
the contents of this log file, but now the requirement is that no one
should not be able to open this file from Windows Explorer. (Currently
if we open the file using some editor, the encrypted contents are
displayed)

The file should only be read/modified from within the program.

Is it possible to achieve this functionality in C#? Can i apply some
password to this file from within my program?

Any help in this regard is greatly appreciated.
Regards,
Ankit!!
Dec 11 '07 #4

P: n/a
If the contents of the log file are already encrypted, what is the purpose
of
enforcing some policy that says nobody is allowed to load the file from
the
filesystem?
Because for highly sensitive information in particular, it's a security
breach just to let someone know it's even there.
Dec 11 '07 #5

P: n/a
On Tue, 11 Dec 2007 06:50:32 -0800 (PST), aa*******@gmail.com wrote:
>Hi,

I want to create a log file from my C# code. Currently I am encrypting
the contents of this log file, but now the requirement is that no one
should not be able to open this file from Windows Explorer. (Currently
if we open the file using some editor, the encrypted contents are
displayed)

The file should only be read/modified from within the program.

Is it possible to achieve this functionality in C#? Can i apply some
password to this file from within my program?

Any help in this regard is greatly appreciated.
Regards,
Ankit!!
As an alternative you could consider steganography. Disguise the
encrypted file inside another file, say a .jpg file. When the .jpg
file is opened in Explorer the user just gets the JPG image. When
your application opens a JPG file it knows to look for the hidden
file, extract it and decrypt it.

That might meet your needs. The other users see a file, but not the
file that you want to conceal.

rossum

Dec 12 '07 #6

P: n/a
From: Larry Smith [mailto:no_spam@_nospam.com]
Posted At: Wednesday, 12 December 2007 2:55 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: How to create password protected files thru C#
Subject: Re: How to create password protected files thru C#
If the contents of the log file are already encrypted, what is the
purpose
of
enforcing some policy that says nobody is allowed to load the file
from
the
filesystem?

Because for highly sensitive information in particular, it's a
security
breach just to let someone know it's even there.
In that case, they would still be able to know it is still there if they
are only able to not open it, as the OP requested.

However, I would tend to agree with you on the reasoning, it just
doesn't agree with the OP.

Dec 12 '07 #7

P: n/a
Hi,

You cannot simply say password protect a file whenever you open it in
notepad. That isn't possible. If you want to password protect a file, you
would need to embed a password within the file. For instance...

MyFile RunSecureFileManager -EnterPassword or
GeneratePublicPrivateKey -GenerateSecureFile - SecuredMyFile

You would have to figure out a proper encryption algorithm to use (there are
many outthere). What you could do is private, public keys and include the
public key within the file while encrypting the data, and decrypt it with
the private key within your program.

That is the only way I could think of doing this.

Good Luck!

--
Regards,
Mohamed Mansour
Microsoft Student Partner

"rossum" <ro******@coldmail.comwrote in message
news:af********************************@4ax.com...
On Tue, 11 Dec 2007 06:50:32 -0800 (PST), aa*******@gmail.com wrote:
>>Hi,

I want to create a log file from my C# code. Currently I am encrypting
the contents of this log file, but now the requirement is that no one
should not be able to open this file from Windows Explorer. (Currently
if we open the file using some editor, the encrypted contents are
displayed)

The file should only be read/modified from within the program.

Is it possible to achieve this functionality in C#? Can i apply some
password to this file from within my program?

Any help in this regard is greatly appreciated.
Regards,
Ankit!!
As an alternative you could consider steganography. Disguise the
encrypted file inside another file, say a .jpg file. When the .jpg
file is opened in Explorer the user just gets the JPG image. When
your application opens a JPG file it knows to look for the hidden
file, extract it and decrypt it.

That might meet your needs. The other users see a file, but not the
file that you want to conceal.

rossum
Dec 12 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.