Преглед изворни кода

2006-09-21 Atsushi Enomoto <[email protected]>

	* EncryptedXml.cs : implement orthodox padding on encryption.

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


svn path=/trunk/mcs/; revision=65800
Atsushi Eno пре 19 година
родитељ
комит
614d7f4d2f

+ 4 - 0
mcs/class/System.Security/System.Security.Cryptography.Xml/ChangeLog

@@ -1,3 +1,7 @@
+2006-09-21  Atsushi Enomoto  <[email protected]>
+
+	* EncryptedXml.cs : implement orthodox padding on encryption.
+
 2006-09-21  Atsushi Enomoto  <[email protected]>
 
 	* EncryptedXml.cs :

+ 11 - 0
mcs/class/System.Security/System.Security.Cryptography.Xml/EncryptedXml.cs

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

+ 4 - 0
mcs/class/System.Security/Test/System.Security.Cryptography.Xml/ChangeLog

@@ -1,3 +1,7 @@
+2006-09-21  Atsushi Enomoto  <[email protected]>
+
+	* EncryptedXmlTest.cs : added roundtrip sample i.e. encryption test.
+
 2006-09-21  Atsushi Enomoto  <[email protected]>
 
 	* EncryptedXmlTest.cs, EncryptedXmlSample2.xml :

+ 61 - 0
mcs/class/System.Security/Test/System.Security.Cryptography.Xml/EncryptedXmlTest.cs

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