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

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

问题描述

我需要两种方法来加密,一个解密的XML文件一键=Hello World的,关键的hello world应该用于加密和解密的XML file.These方法应该在所有的机器工作! !任何加​​密方法是不行的。下面的XML文件的内容:

 <根和GT; 
<&里克GT;
<&号GT; 19834209< /数字>
<期限和GT; 02/02/2002< /到期>
< / LIC>
< /根>



能否一些给我一个样品?这个问题是MSDN示例encyptions使XML文件已加密,但是当我解密另一台机器上不工作。对于例如



我试过这个示例:
如何:使用非对称密钥
,但这里加密XML元素有一些还挺会话和另一台机器上它说坏的数据phewf

解决方案

如果您想进行加密和解密,你应该使用对称的方法(这是定义,真的)相同的密钥。这里是最接近的一个样品(同一个源)。
http://msdn.microsoft.com/en-us/library /sb7w85t6.aspx



张贴的样品并不是因为他们没有使用相同的密钥工作。不仅在不同的机器:运行在同一台机器上的程序两次不应该工作,要么(没有为我工作),因为它们使用不同的随机密钥每次结果
尝试创建之后加入该代码的键:

 键=新RijndaelManaged的(); 

字符串密码=Password1234; //这里密码
字节[] = saltBytes Encoding.UTF8.GetBytes(盐); //这里盐(另一个字符串)
变种P =新Rfc2898DeriveBytes(密码,saltBytes); // TODO:想想迭代次数(第三个参数)
//大小由8 devided因为[1字节= 8比特]
key.IV = p.GetBytes(key.BlockSize / 8) ;
key.Key = p.GetBytes(key.KeySize / 8);

现在的程序是使用相同的密钥和初始向量,加密和解密应在所有计算机上工作。结果
另外,考虑重新命名算法,否则这是非常误导的。我会说这是从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天全站免登陆