简体   繁体   中英

encrypt and decrypt xml file content

I have large xml file with more than 30 000 lines. 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. 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).

What method should I use to encrypt and decrypt xml file content?

I was thinking to use 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...

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...

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...

.Net provides many crypto-systems. Depending on your needs you can chose between DES, 3DES, AES or RSA(which is in efficient in your case). DES is the least secure, 3DES is better but I'd go for the 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);
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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