2006-09-21 Atsushi Enomoto <atsushi@ximian.com>
authorAtsushi Eno <atsushieno@gmail.com>
Fri, 22 Sep 2006 01:16:12 +0000 (01:16 -0000)
committerAtsushi Eno <atsushieno@gmail.com>
Fri, 22 Sep 2006 01:16:12 +0000 (01:16 -0000)
* EncryptedXml.cs : implement orthodox padding on encryption.

* EncryptedXmlTest.cs : added roundtrip sample i.e. encryption test.

svn path=/trunk/mcs/; revision=65800

mcs/class/System.Security/System.Security.Cryptography.Xml/ChangeLog
mcs/class/System.Security/System.Security.Cryptography.Xml/EncryptedXml.cs
mcs/class/System.Security/Test/System.Security.Cryptography.Xml/ChangeLog
mcs/class/System.Security/Test/System.Security.Cryptography.Xml/EncryptedXmlTest.cs

index 0f5c862445a315a39e439ff0569828aa2aa4b457..ae06b9ff0d9914b79dc9e6ddc137ce5776b79366 100644 (file)
@@ -1,3 +1,7 @@
+2006-09-21  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * EncryptedXml.cs : implement orthodox padding on encryption.
+
 2006-09-21  Atsushi Enomoto  <atsushi@ximian.com>
 
        * EncryptedXml.cs :
index b2e95594d2d92c570105e557c8e8a1339f17bbe5..3509a688a91062dabcfdd98986cdc475f90c07e6 100644 (file)
@@ -237,6 +237,17 @@ namespace System.Security.Cryptography.Xml {
                }
 
                public byte[] EncryptData (byte[] plainText, SymmetricAlgorithm symAlg)
+               {
+                       PaddingMode bak = symAlg.Padding;
+                       try {
+                               symAlg.Padding = PaddingMode.ISO10126;
+                               return EncryptDataCore (plainText, symAlg);
+                       } finally {
+                               symAlg.Padding = bak;
+                       }
+               }
+
+               byte[] EncryptDataCore (byte[] plainText, SymmetricAlgorithm symAlg)
                {
                        // Write the symmetric algorithm IV and ciphertext together.
                        // We use a memory stream to accomplish this.
index be98805a944f2e8331ae8d8c3d30046feea64294..faca2e89b54f764a0d830a02ace94d72f6efcafb 100644 (file)
@@ -1,3 +1,7 @@
+2006-09-21  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * EncryptedXmlTest.cs : added roundtrip sample i.e. encryption test.
+
 2006-09-21  Atsushi Enomoto  <atsushi@ximian.com>
 
        * EncryptedXmlTest.cs, EncryptedXmlSample2.xml :
index 50b7213d1f2153537c7c0f507af5c5db7de8350b..e73ee63973bcde6eff4ca2654298d4a3ad8f2353 100644 (file)
@@ -11,6 +11,7 @@
 
 using System;
 using System.Collections;
+using System.IO;
 using System.Security.Cryptography;
 using System.Security.Cryptography.X509Certificates;
 using System.Security.Cryptography.Xml;
@@ -69,6 +70,66 @@ namespace MonoTests.System.Security.Cryptography.Xml
                        edata.LoadXml (doc.DocumentElement);
                        encxml.ReplaceData (doc.DocumentElement, encxml.DecryptData (edata, aes));
                }
+
+               [Test]
+               public void RoundtripSample1 ()
+               {
+                       StringWriter sw = new StringWriter ();
+
+                       // Encryption
+                       {
+                               XmlDocument doc = new XmlDocument ();
+                               doc.PreserveWhitespace = true;
+                               doc.LoadXml ("<root>  <child>sample</child>   </root>");
+
+                               XmlElement body = doc.DocumentElement;
+
+                               RijndaelManaged aes = new RijndaelManaged ();
+                               aes.Mode = CipherMode.CBC;
+                               aes.KeySize = 256;
+                               aes.IV = Convert.FromBase64String ("pBUM5P03rZ6AE4ZK5EyBrw==");
+                               aes.Key = Convert.FromBase64String ("o/ilseZu+keLBBWGGPlUHweqxIPc4gzZEFWr2nBt640=");
+                               aes.Padding = PaddingMode.Zeros;
+
+                               EncryptedXml exml = new EncryptedXml ();
+                               byte [] encrypted = exml.EncryptData (body, aes, false);
+                               EncryptedData edata = new EncryptedData ();
+                               edata.Type = EncryptedXml.XmlEncElementUrl;
+                               edata.EncryptionMethod = new EncryptionMethod (EncryptedXml.XmlEncAES256Url);
+                               EncryptedKey ekey = new EncryptedKey ();
+                               // omit key encryption, here for testing
+                               byte [] encKeyBytes = aes.Key;
+                               ekey.CipherData = new CipherData (encKeyBytes);
+                               ekey.EncryptionMethod = new EncryptionMethod (EncryptedXml.XmlEncRSA15Url);
+                               DataReference dr = new DataReference ();
+                               dr.Uri = "_0";
+                               ekey.AddReference (dr);
+                               edata.KeyInfo.AddClause (new KeyInfoEncryptedKey (ekey));
+                               edata.KeyInfo = new KeyInfo ();
+                               ekey.KeyInfo.AddClause (new RSAKeyValue (RSA.Create ()));
+                               edata.CipherData.CipherValue = encrypted;
+                               EncryptedXml.ReplaceElement (doc.DocumentElement, edata, false);
+                               doc.Save (new XmlTextWriter (sw));
+                       }
+
+                       // Decryption
+                       {
+                               RijndaelManaged aes = new RijndaelManaged ();
+                               aes.Mode = CipherMode.CBC;
+                               aes.KeySize = 256;
+                               aes.Key = Convert.FromBase64String (
+                                       "o/ilseZu+keLBBWGGPlUHweqxIPc4gzZEFWr2nBt640=");
+                               aes.Padding = PaddingMode.Zeros;
+
+                               XmlDocument doc = new XmlDocument ();
+                               doc.PreserveWhitespace = true;
+                               doc.LoadXml (sw.ToString ());
+                               EncryptedXml encxml = new EncryptedXml (doc);
+                               EncryptedData edata = new EncryptedData ();
+                               edata.LoadXml (doc.DocumentElement);
+                               encxml.ReplaceData (doc.DocumentElement, encxml.DecryptData (edata, aes));
+                       }
+               }
        }
 }
 #endif