How to create password protected files thru C#

A

aagarwal8

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!!
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
A

AA2e72E

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.
 
P

Peter Bromberg [C# MVP]

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
 
L

Larry Smith

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.
 
R

rossum

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
 
W

Wingot

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#


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.
 
M

Mohamed Mansour

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top