Exemple XMLENCRYPT
XmlEncrypt est un standard qui permet d'échanger des données chiffrées au format xml.
L'exemple ci-dessous permet de chiffrer une partie d'un fichier xml dans un programme de type console
et de déchiffrer dans un autre programme de type Winform.
Programme console de chiffrement
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
namespace cryptage
{ class Program
{
static void Main(string[] args)
{ XmlDocument xmlDoc = new XmlDocument();
// Charge le fichier XML en clair dans un objet XmlDocument.
try
{
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load("c:\\crypto\\test.xml");
} catch (Exception e)
{ Console.WriteLine(e.Message);
} // Création d'un clé RSA. Cette clé servira à chiffrer la clé symétrique qui sera embarquée,
// dans le fichier xml.
RSA rsaKey = new RSACryptoServiceProvider();
XmlDocument xmlKey = new XmlDocument();
xmlKey.LoadXml(rsaKey.ToXmlString(true));
xmlKey.Save("c:\\crypto\\key.xml");
try
{
// Chiffrement de l'élément XML.
Encrypt(xmlDoc, "creditcard", rsaKey, "rsaKey");
// Sauvegarde du fichier chiffré xmlDoc.Save("c:\\crypto\\test2.xml"); ;
} catch (Exception e)
{ Console.WriteLine(e.Message);
} finally
{
// Clear the RSA key.
rsaKey.Clear();
}
}
public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, RSA Alg, string KeyName)
{ // Contrôle des arguments.
if (Doc == null)
throw new ArgumentNullException("Doc");
if (ElementToEncrypt == null)
throw new ArgumentNullException("ElementToEncrypt");
if (Alg == null)
throw new ArgumentNullException("Alg");
////////////////////////////////////////////////
// Recherche de l'élément à chiffrer
////////////////////////////////////////////////
XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementToEncrypt)[0] as XmlElement;
if (elementToEncrypt == null)
{ throw new XmlException("The specified element was not found");
} //////////////////////////////////////////////////
//Création d'une nouvelle instance EncryptedXml pour chiffrer l'XmlElement
// avec une clé symétrique aléatoire.
//////////////////////////////////////////////////
// Création d'une clé de 256 bits.
RijndaelManaged sessionKey = new RijndaelManaged();
sessionKey.KeySize = 256; EncryptedXml eXml = new EncryptedXml();
byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, sessionKey, false);
////////////////////////////////////////////////
// Construction d'un objet EncryptedData et renseignement des informations de chiffrement
////////////////////////////////////////////////
EncryptedData edElement = new EncryptedData();
edElement.Type = EncryptedXml.XmlEncElementUrl;
// Creation d'une EncryptionMethod
edElement.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url);
//Chiffrement de la clé de session et rajout à l'élément EncryptedKey
EncryptedKey ek = new EncryptedKey();
byte[] encryptedKey = EncryptedXml.EncryptKey(sessionKey.Key, Alg, false);
ek.CipherData = new CipherData(encryptedKey);
ek.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncRSA15Url);
// Create a new KeyInfoName element.
KeyInfoName kin = new KeyInfoName();
// Specify a name for the key.
kin.Value = KeyName;
// Add the KeyInfoName element to the
// EncryptedKey object.
ek.KeyInfo.AddClause(kin);
// Add the encrypted key to the
// EncryptedData object.
edElement.KeyInfo.AddClause(new KeyInfoEncryptedKey(ek));
// Add the encrypted element data to the
// EncryptedData object.
edElement.CipherData.CipherValue = encryptedElement;
////////////////////////////////////////////////////
// Replace the element from the original XmlDocument
// object with the EncryptedData element.
////////////////////////////////////////////////////
EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false);
} } } Programme Winform de déchiffrement using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Security.Cryptography; using System.Security.Cryptography.Xml; using System.Xml; namespace clnCrypto
{ public partial class Form1 : Form { public Form1()
{ InitializeComponent(); } private void Form1_Load(object sender, EventArgs e)
{ RSA rsaKey = new RSACryptoServiceProvider();
XmlDocument xmlkey = new XmlDocument();
xmlkey.Load("c:\\crypto\\key.xml");
rsaKey.FromXmlString(xmlkey.OuterXml); XmlDocument xmlDoc = new XmlDocument();
// Load an XML file into the XmlDocument object. //xmlDoc.PreserveWhitespace = true; xmlDoc.Load("c:\\crypto\\test2.xml");
this.richTextBox1.Text = xmlDoc.InnerXml;
Decrypt(xmlDoc, rsaKey, "rsaKey");
this.richTextBox2.Text = xmlDoc.InnerXml;
} private static void Decrypt(XmlDocument Doc, RSA Alg, string KeyName)
{ // Check the arguments. if (Doc == null)
throw new ArgumentNullException("Doc");
if (Alg == null)
throw new ArgumentNullException("Alg");
if (KeyName == null)
throw new ArgumentNullException("KeyName");
// Create a new EncryptedXml object. EncryptedXml exml = new EncryptedXml(Doc);
exml.AddKeyNameMapping(KeyName, Alg); // Decrypt the element using the symmetric key. try { exml.DecryptDocument(); } catch (System.Security.Cryptography.CryptographicException e)
{ Console.WriteLine(e.Message);
} } } } Fichier xml en clair <?xml version="1.0" encoding="utf-8" ?>
<root>
<creditcard>
<number>19834209</number>
<expiry>02/02/2002</expiry>
</creditcard>
</root> fichier xml chiffré <?xml version="1.0" encoding="utf-8"?>
<root>
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#"><EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc" /><KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"><EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#"><EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" /><KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"><KeyName>rsaKey</KeyName></KeyInfo><CipherData><CipherValue>PbpKLi/EBGoh6qTxUJyRPYat/u+1Rj5g+H6IxIWRXJ3E6cODsKxJbFokzg980+OBFKh6ZAagmrwxATRElX1LMgDZfatCBEW3jIaESG12N0HnPPlntmY5PViqZpny3lm7XltAISeFE7ovxShKFcxU9749uC+Zj+eONlhJUGt5DPQ=</CipherValue></CipherData></EncryptedKey></KeyInfo><CipherData><CipherValue>9fFBfVMGS50njNrt+vJtWKrgEgciG93Py+RkVnW4IWyRzvDx9VoDrEEs7kRPqN5PCAgIt2Y9TnExX6e+eWQM9JlReTAKlJ+RFY3No2oc1APElYcH2JJdpkj4V2XpOLduYOIwK108p2eGJiliO2L1bw==</CipherValue></CipherData></EncryptedData>
</root>
Mapping O/R : PUZZLE
Un framework intéressant : Puzzle.
Puzzle comprend :
- ObjectMapper : Outil permettant de générer les POCOs et la couche de persistance avec NPersist ou
NHibernate;
- NPersist : un framework O/R mapping;
- NPath : query sur les graphes en mémoire (objets);
- NAspect : un framework AOP
- NFactory
Tout cela en Opensource et téléchargeable sur le site :
http://www.puzzleframework.com/
Site sur WCF (Indigo)
Microsoft vient d'ouvrir un site dédié à WCF.
http://windowscommunication.net/
Il y a pas mal d'articles intéressants dont des exemples pour débuter.