// (c) 2006 Richard Grimes // www.grimes.demon.co.uk using System; using System.Security.Cryptography; using System.Security.Cryptography.Xml; using System.Xml; using System.IO; class App { // parameters: e|d infile outfile passphrase static void Main(string[] args) { if (args.Length < 4) { Console.WriteLine("Usage: symm e|d infile outfile passphrase"); return; } bool bEncrypt = (args[0][0] == 'e'); string infile = args[1]; if (!File.Exists(infile)) { Console.WriteLine("{0} does not exist", infile); return; } string outfile = args[2]; if (File.Exists(outfile)) { File.Delete(outfile); } string passphrase = args[3]; Rijndael key = new RijndaelManaged(); PasswordDeriveBytes pdb = new PasswordDeriveBytes(passphrase, null); key.Key = pdb.GetBytes(key.KeySize / 8); if (bEncrypt) { Encrypt(infile, outfile, key); } else { Decrypt(infile, outfile, key); } } static void Encrypt(string infile, string outfile, SymmetricAlgorithm key) { XmlDocument doc = new XmlDocument(); doc.Load(infile); XmlElement elem = (XmlElement)doc.GetElementsByTagName("CreditCard")[0]; EncryptedXml encXml = new EncryptedXml(); byte[] enc = encXml.EncryptData(elem, key, false); EncryptedData ed = new EncryptedData(); ed.Type = EncryptedXml.XmlEncElementUrl; ed.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url); ed.CipherData.CipherValue = enc; EncryptedXml.ReplaceElement(elem, ed, false); doc.Save(outfile); } static void Decrypt(string infile, string outfile, SymmetricAlgorithm key) { } }