简体   繁体   English

加密和解密xml文件内容

[英]encrypt and decrypt xml file content

I have large xml file with more than 30 000 lines. 我有超过3万行的大型xml文件。 It has content like 它的内容像

<?xml version="1.0"?>
   <Nodes>
      <Node>some node name </Node>
      <Node>some node name 2 </Node>
      ...
   </Nodes>

I want to send this xml file with encrypted content to the client manually. 我想将此带有加密内容的xml文件手动发送到客户端。 Client app (wpf) will load this file and encrypt this file on demand without user intervention (all possible keys will be predefined earlier on this client app). 客户端应用(wpf)将在无需用户干预的情况下按需加载此文件并对该文件加密(所有可能的密钥将在此客户端应用上预先定义)。

What method should I use to encrypt and decrypt xml file content? 我应该使用哪种方法来加密和解密xml文件内容?

I was thinking to use http://aspnettutorialonline.blogspot.com/2012/05/encryption-and-decryption-in-aspnet.html 我正在考虑使用http://aspnettutorialonline.blogspot.com/2012/05/encryption-and-decryption-in-aspnet.html

but since I do not have much experience with this subject I'm asking is this good solution or you would recommend something else? 但是由于我在该主题上没有太多经验,所以我想问这是一个好的解决方案,还是您会建议其他建议?

AES encryption is very easy with .NET... NET可以很容易地进行AES加密...

private readonly ICryptoTransform encryptor;
private readonly ICryptoTransform decryptor;
private readonly UTF8Encoding encoder;

var rm = new RijndaelManaged();
encryptor = rm.CreateEncryptor(key, vector);
decryptor = rm.CreateDecryptor(key, vector);
encoder = new UTF8Encoding();

public string Encrypt(string unencrypted)
{
    return Convert.ToBase64String(Encrypt(encoder.GetBytes(unencrypted)));         
}

public byte[] Encrypt(byte[] buffer)
{
    var encryptStream = new MemoryStream();
    using (var cs = new CryptoStream(encryptStream, encryptor, CryptoStreamMode.Write))
    {
        cs.Write(buffer, 0, buffer.Length);
    }
    return encryptStream.ToArray();
}

key and vector are byte[] arrays as expected by the RijndaelManaged.CreateEncryptor() and RijndaelManaged.CreateDecryptor() methods... keyvectorRijndaelManaged.CreateEncryptor()RijndaelManaged.CreateDecryptor()方法所期望的byte []数组。

The key and vector values will end up being a part of your client app code so hiding the values and obfuscating will protect only against non-sofisticated attackers but if all you need is to hide the xml contents from the non-technical end-users that might be sufficient... keyvector量值最终将成为客户端应用程序代码的一部分,因此隐藏值和混淆将仅防止非复杂的攻击者,但是,如果您需要的只是对非技术性最终用户隐藏xml内容,可能就足够了...

.Net provides many crypto-systems. .Net提供了许多密码系统。 Depending on your needs you can chose between DES, 3DES, AES or RSA(which is in efficient in your case). 根据您的需要,您可以在DES,3DES,AES或RSA之间选择(这对您而言很有效)。 DES is the least secure, 3DES is better but I'd go for the AES. DES最不安全,3DES更好,但我会选择AES。 to Encrypt: 加密:

using System.Security.Cryptography;
...
class AES {
   private AesCryptoServiceProvider aes;
   public AES (Byte[] IV, Byte[] Key) {
       aes = AesCryptoServiceProvider();
       aes.Key = Key; // 256 Bits Long
       // AES Key can be generated using SHA256
       aes.IV = IV; // 128 Bits Long
       // IV can be generated using MD5
   }
   public Byte[] Encrypt(Byte[] FileStream) {
       ICryptoTransform Transform = aes.CreateEncryptor();
       return Transform.TransformFinalBlock(FileStream, 0, FileStream.Lenght);
   }     
   public Byte[] Decrypt (Byte[] FileStream){
       ICryptoTransform Transform = aes.CreateDecryptor();
       return Transform.TransformFinalBlock(FileStream, 0, FileStream.Lenght);
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM