C#加密XML文件 [英] C# Encrypt an XML File

查看:129
本文介绍了C#加密XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要两个加密方法,一个用key =hello world来解密一个xml文件,这个关键的hello world应该用来加密和解密xml文件。这些方法应该在所有的机器上工作! !任何加密方式都可以。 XML文件内容如下:

 < root> 
< lic>
< number> 19834209< / number>
< expiry> 02/02/2002< / expiry>
< / lic>
< / root>

有些可以给我一个示例吗?问题是msdn示例encyptions使一个xml文件被察觉,但是我在其他机器上解密它不工作。例如



我尝试了这个示例:
如何:使用非对称密钥加密XML元素
但是这里有一些类型的会话,而在另一台机器上它表示不好数据phewf!

解决方案

如果你想要加密和解密的相同密钥,你应该使用对称方法(这是定义,真的)。这是你最近的一个样本(同一个来源)。



发布的示例不起作用,因为它们不使用相同的键。不仅在不同的机器上:在同一机器上运行程序两次也不应该工作(对我来说不行),因为每次使用不同的随机密钥。

尝试在创建您的键:

  key = new RijndaelManaged(); 

string password =Password1234; // password here
byte [] saltBytes = Encoding.UTF8.GetBytes(Salt); // salt here(另一个字符串)
var p = new Rfc2898DeriveBytes(password,saltBytes); // TODO:考虑迭代次数(第三个参数)
// size由8个字节决定,因为[1字节= 8位]
key.IV = p.GetBytes(key.BlockSize / 8) ;
key.Key = p.GetBytes(key.KeySize / 8);

现在程序使用相同的键和初始向量,加密和解密应该在所有机器上工作。

另外,考虑将 key 重命名为算法,否则这是非常误导的。我会说这是MSDN的一个不好的,不正常的例子。




注意 PasswordDeriveBytes.GetBytes()由于 PasswordDeriveBytes 类。上面的代码已被重写为使用更安全的 Rfc2898DeriveBytes 类(PBKDF2而不是PBKDF1)。使用 PasswordDeriveBytes 生成的代码可能会受到损害。



另请参见:使用PKBDF2-SHA256时推荐的迭代次数?


I need two methods one to encrypt and one to decrypt an xml file with a key= "hello world",the key hello world should be used to encrypt and decrypt the xml file.These methods should work on all machines!!! Any encryption methods will do. XML File contents below:

<root>
    <lic>
        <number>19834209</number>
        <expiry>02/02/2002</expiry>
    </lic>
</root>

Can some give me a sample?The issue is the msdn sample encyptions make a xml file encypted but when I decrypt on another machine it doesn't work.For example

I tried this sample: How to: Encrypt XML Elements with Asymmetric Keys, but here there is some kinda session and on another machine it says bad data phewf!

解决方案

If you want the same key for encrypting and decrypting you should use a symmetric method (that's the definition, really). Here's the closest one to your sample (same source). http://msdn.microsoft.com/en-us/library/sb7w85t6.aspx

The posted sample isn't working because they aren't using the same keys. Not only on different machines: running the program on the same machine twice should not work either (didn't work for me), because they use different random keys every time.
try adding this code after creating your key:

key = new RijndaelManaged();

string password = "Password1234"; //password here
byte[] saltBytes = Encoding.UTF8.GetBytes("Salt"); // salt here (another string)
var p = new Rfc2898DeriveBytes(password, saltBytes); //TODO: think about number of iterations (third parameter)
// sizes are devided by 8 because [ 1 byte = 8 bits ]
key.IV = p.GetBytes(key.BlockSize / 8);
key.Key = p.GetBytes(key.KeySize / 8);

Now the program is using the same key and initial vector, and Encrypt and Decrypt should work on all machines.
Also, consider renaming key to algorithm, otherwise this is very misleading. I'd say it's a bad, not-working-well example from MSDN.

NOTE: PasswordDeriveBytes.GetBytes() has been deprecated because of serious (security) issues within the PasswordDeriveBytes class. The code above has been rewritten to use the safer Rfc2898DeriveBytes class instead (PBKDF2 instead of PBKDF1). Code generated with the above using PasswordDeriveBytes may be compromised.

See also: Recommended # of iterations when using PKBDF2-SHA256?

这篇关于C#加密XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆