Bonjour,
Après pas mal d'absence je reprends le développement.
J'ai un exemple de fichier xml comme suis:
Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 <?xml version="1.0" encoding="utf-8" ?> <root> <creditcard> <number>19834209</number> <expiry>02/02/2002</expiry> </creditcard> </root>
J'essaie de chiffrer donc la partieVoilà le code que j'ai trouvé:<creditcard>
La méthode Encrypt:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 using System; using System.Windows.Forms; using System.Xml; using System.Security.Cryptography; using System.Security.Cryptography.Xml; namespace TestCryptage { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Aes key = null; try { // Create a new AES key. key = Aes.Create(); // Load an XML document. XmlDocument xmlDoc = new XmlDocument(); xmlDoc.PreserveWhitespace = true; xmlDoc.Load("Test.xml"); // Encrypt the "creditcard" element. Encrypt(xmlDoc, "creditcard", key); //Decrypt(xmlDoc, key); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { // Clear the key. if (key != null) { key.Clear(); } } }
Quand je test le code aucun problème mais quand j'ouvre l'Xml, rien n'est chiffré !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public static void Encrypt(XmlDocument Doc, string ElementName, SymmetricAlgorithm Key) { // Check the arguments. if (Doc == null) throw new ArgumentNullException("Doc"); if (ElementName == null) throw new ArgumentNullException("ElementToEncrypt"); if (Key == null) throw new ArgumentNullException("Alg"); //////////////////////////////////////////////// // Find the specified element in the XmlDocument // object and create a new XmlElement object. //////////////////////////////////////////////// XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementName)[0] as XmlElement; // Throw an XmlException if the element was not found. if (elementToEncrypt == null) { throw new XmlException("The specified element was not found"); } ////////////////////////////////////////////////// // Create a new instance of the EncryptedXml class // and use it to encrypt the XmlElement with the // symmetric key. ////////////////////////////////////////////////// EncryptedXml eXml = new EncryptedXml(); byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, Key, false); //////////////////////////////////////////////// // Construct an EncryptedData object and populate // it with the desired encryption information. //////////////////////////////////////////////// EncryptedData edElement = new EncryptedData(); edElement.Type = EncryptedXml.XmlEncElementUrl; // Create an EncryptionMethod element so that the // receiver knows which algorithm to use for decryption. // Determine what kind of algorithm is being used and // supply the appropriate URL to the EncryptionMethod element. string encryptionMethod = null; if (Key is Aes) { encryptionMethod = EncryptedXml.XmlEncAES256Url; } else { // Throw an exception if the transform is not AES throw new CryptographicException("The specified algorithm is not supported or not recommended for XML Encryption."); } edElement.EncryptionMethod = new EncryptionMethod(encryptionMethod); // 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); }
Quelqu'un aurait une piste, ça fait 2 jours que je parcours des blogs, des tutos, etc...
J'ai chopé le code ici (Doc microsoft)...
Merci d'avances,
Cordialement,
Chtulus
Partager