使用 gzip 密码保护 xml 文件

发布于 2024-11-09 16:00:37 字数 1572 浏览 4 评论 0原文

好了,伙计们,现在是时候回答这个古老的问题了,如何使用 C# 来密码保护 xml 文件? 我实际上也在 C# 中创建了该文件(似乎不是相关的),现在我需要密码保护它,以便我可以通过电子邮件将其发送给客户,任何建议,

我也尝试将 xml 文件放入 zip 文件中,使用 C#,执行此操作后,文件会丢失其扩展名,并且它会使用我找到的每种方法执行此操作,因此我真的只想用密码保护原始文件。


我应该更清楚这一点,该文件永久丢失其扩展名,当最终用户解压它时,它不再是一个 xml 文件,它只是一个带有名称的文件,没有关联或任何

可以更改的东西,它是很多人指出,xml 没有受到密码保护,因为它只是文本,不是问题,所以让我们更改一下它的压缩方式,

    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();
    } 

上面的代码会压缩文件,但是当最终用户解压缩文件时该文件丢失了它的 xml 扩展名及其文件关联,

好的,我有一个更新,我想出了如何防止文件丢失它的扩展名,如果我将输出文件名更改为sample.xml.zip,系统可以很好地处理它,授予输出文件就像这样,sample.xml.zip,但是winzip从不抱怨打开它,7zip也没有,所以我对此非常满意,现在受密码保护的东西是我还没有弄清楚的东西然而。

我的新代码仅供参考。

    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();
    } 

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 would 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();
    } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乞讨 2024-11-16 16:00:38

执行此操作后,文件会丢失
这是扩展名

是什么意思,文件名从 MyXMLFile.xml 更改为 MyXMLFile.zip
你对此无能为力,绝对无能为力。

xml 文件是纯文本文件,如果不以某种方式对其进行加密,则无法使用密码保护该文件。一旦加密,它就不再是 Xml 文件,而是加密文件,解密后将生成 Xml 文件。

将 xml 文件加密到受密码保护的 Zip 文件中是解决此问题的完美解决方案。

一旦最终用户解压 zip 文件,他们就会将其视为 Xml 文件,然后一切都会正常。

希望这有帮助。

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.

你的呼吸 2024-11-16 16:00:38

您无法像使用 Word 文档那样对 XML 文件进行密码保护。您可以在 Word 文档上设置密码的原因是因为 Word 以及其他可以读取 Word 文档的程序都支持密码保护。没有什么可以阻止程序完全忽略密码(除非使用密码作为密钥生成器以某种方式对文件进行加密)。

XML 文件只是文本文件。如果不将它们放入受密码保护的容器(例如 zip 文件)中,则无法进行密码保护。当您压缩 XML 文件时,它会被放置在一个 zip 存档中,扩展名为 .zip 以表明它是一个 zip 文件。

然后,接收 zip 文件的人需要提供正确的密码才能解压缩 zip 文件并检索原始 XML 文件。

我不相信 .NET 对管理 .zip 文件有任何支持。您可以使用 DotNetZip 等第三方库来帮助您完成此操作。

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.

你怎么敢 2024-11-16 16:00:38

加密文件然后解密是一种选择。 本文提供了有关加密和解密的一些信息。

Encrypting the file and then decrypting it would be one option. This article give some information on encrypting and decrypting.

分开我的手 2024-11-16 16:00:38

我认为一个不错的选择是将其压缩并用密码保护 zip 文件。不是 XML。像 dotnetzip 这样的库可以解决这个问题,而且非常简单。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文