Password protecting an xml file using gzip
All right guys and gals it's time for the age old question, how do you password protect an xml file using C#? I have acutally created the file in C# as well (not as if that is relevant) and now I need to password protect it so I can email it out to clients, any suggestions guys,
Also I tried putting the xml file into a zip file, using C# and upon doing this the file loses its extension, and it does this with every method I find, so I wou开发者_运维百科ld really just like to password protect the original file.
I should have been more clear on this, the file loses it's extension permanetly, when the end user unzips it, it's no longer an xml file, it's just a file with a name, no association or any thing
ok changing this a bit, it's been pointed out a lot that xml doesn't get password protected because it's just text, not a problem, so lets change this up how about the ziping of it
FileStream sourceFile = File.OpenRead(@"C:\sample.xml");
FileStream destFile = File.Create(@"C:\sample.zip");
GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress);
try
{
int theByte = sourceFile.ReadByte();
while (theByte != -1)
{
compStream.WriteByte((byte)theByte);
theByte = sourceFile.ReadByte();
}
}
finally
{
compStream.Dispose();
}
this code above zips the file, but when the file is unziped by the end user the file loses it's xml extension and with it it's file association
ok i have an update i figured out how to keep the file from losing it's extension, if i change the output file name to sample.xml.zip, the system handles it fine, granted the output file comes out reading just like this, sample.xml.zip, but winzip never bitches about opening it, neither does 7zip so i'm perfectly happy with this, now the password protected thing is something i haven't figured out yet.
just for reference sake, my new code.
FileStream sourceFile = File.OpenRead(@"C:\sample.xml");
FileStream destFile = File.Create(@"C:\sample.xml.zip");
GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress);
try
{
int theByte = sourceFile.ReadByte();
while (theByte != -1)
{
compStream.WriteByte((byte)theByte);
theByte = sourceFile.ReadByte();
}
}
finally
{
compStream.Dispose();
}
and upon doing this the file looses it's extension
What do you mean, the file name changes from MyXMLFile.xml
to MyXMLFile.zip
?
There's nothing you can do about that, absolutely nothing.
An xml file is a plain text file, you can't password protect the file without somehow encrypting it. Once you encrypt it, it's no longer an Xml file, it's an encrypted file, that when decrypted will produce an Xml file.
Encrypting your xml file into a password protected Zip file is a perfectly good solution to this problem.
Once the end user unzips the zip file, they'll see it as an Xml file, and then everything will be ok.
Hope this helps.
You can't password protect an XML file the way you can with a Word document. The reason you can place passwords on Word documents is because Word and presumably other programs which can read Word documents support password protection. Nothing prevents a program from completely ignoring the password (unless the file is somehow encrypted using the password as a key generator).
XML files are simply text files. No password protection is possible without placing them in a password protected container (such as a zip file). When you zip up the XML file, it is placed inside a zip archive with the extension of .zip to indicate that it is a zip file.
It is then up to the person receiving the zip file provide the correct password in order to decompress the zip file and retrieve the original XML file.
I don't believe .NET has any support for managing .zip files. You can use an third-party library like DotNetZip to help you with this.
Encrypting the file and then decrypting it would be one option. This article give some information on encrypting and decrypting.
I think a good option would be to zip it and password protect the zip file. Not the xml. A library like dotnetzip could work for this and is pretty straight forward.
精彩评论