تهیه XML امضاء شده جهت تولید مجوز استفاده از برنامه
نویسنده: وحید نصیری
تاریخ: ۱۳۹۱/۱۰/۲۵ ۸:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
<?xml version="1.0" encoding="utf-8"?>
<license id="17d46246-a6cb-4196-98a0-ff6fc08cb67f" expiration="2012-06-12T00:00:00.0000000" type="Trial" prof="EFProf">
<name>MyName</name>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>b8N0bDE4gTakfdGKtzDflmmyyXI=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>IPELgc9BbkD8smXSe0sGqp5vS57CtZo9ME2ZfXSq/thVu...=</SignatureValue>
</Signature>
</license>
public static string CreateRSAKeyPair(int dwKeySize = 1024)
{
using (var provider = new RSACryptoServiceProvider(dwKeySize))
{
return provider.ToXmlString(includePrivateParameters: true);
}
}
namespace SignedXmlSample
{
public enum LicenseType
{
None,
Trial,
Standard,
Personal
}
}
using System;
using System.Xml.Serialization;
namespace SignedXmlSample
{
public class License
{
[XmlAttribute]
public Guid Id { set; get; }
public string Domain { set; get; }
[XmlAttribute]
public string IssuedTo { set; get; }
[XmlAttribute]
public DateTime Expiration { set; get; }
[XmlAttribute]
public LicenseType Type { set; get; }
}
}
using System;
using System.IO;
using System.Security.Cryptography; // needs a ref. to `System.Security.dll` asm.
using System.Security.Cryptography.Xml;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace SignedXmlSample
{
public static class LicenseGenerator
{
public static string CreateLicense(string licensePrivateKey, License licenseData)
{
using (var provider = new RSACryptoServiceProvider())
{
provider.FromXmlString(licensePrivateKey);
var xmlDocument = createXmlDocument(licenseData);
var xmlDigitalSignature = getXmlDigitalSignature(xmlDocument, provider);
appendDigitalSignature(xmlDocument, xmlDigitalSignature);
return xmlDocumentToString(xmlDocument);
}
}
public static string CreateRSAKeyPair(int dwKeySize = 1024)
{
using (var provider = new RSACryptoServiceProvider(dwKeySize))
{
return provider.ToXmlString(includePrivateParameters: true);
}
}
public static License ReadLicense(string licensePublicKey, string xmlFileContent)
{
var doc = new XmlDocument();
doc.LoadXml(xmlFileContent);
using (var provider = new RSACryptoServiceProvider())
{
provider.FromXmlString(licensePublicKey);
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("sig", "http://www.w3.org/2000/09/xmldsig#");
var xml = new SignedXml(doc);
var signatureNode = (XmlElement)doc.SelectSingleNode("//sig:Signature", nsmgr);
if (signatureNode == null)
throw new InvalidOperationException("This license file is not signed.");
xml.LoadXml(signatureNode);
if (!xml.CheckSignature(provider))
throw new InvalidOperationException("This license file is not valid.");
var ourXml = xml.GetXml();
if (ourXml.OwnerDocument == null || ourXml.OwnerDocument.DocumentElement == null)
throw new InvalidOperationException("This license file is coruppted.");
using (var reader = new XmlNodeReader(ourXml.OwnerDocument.DocumentElement))
{
var xmlSerializer = new XmlSerializer(typeof(License));
return (License)xmlSerializer.Deserialize(reader);
}
}
}
private static void appendDigitalSignature(XmlDocument xmlDocument, XmlNode xmlDigitalSignature)
{
xmlDocument.DocumentElement.AppendChild(xmlDocument.ImportNode(xmlDigitalSignature, true));
}
private static XmlDocument createXmlDocument(License licenseData)
{
var serializer = new XmlSerializer(licenseData.GetType());
var sb = new StringBuilder();
using (var writer = new StringWriter(sb))
{
var ns = new XmlSerializerNamespaces(); ns.Add("", "");
serializer.Serialize(writer, licenseData, ns);
var doc = new XmlDocument();
doc.LoadXml(sb.ToString());
return doc;
}
}
private static XmlElement getXmlDigitalSignature(XmlDocument xmlDocument, AsymmetricAlgorithm key)
{
var xml = new SignedXml(xmlDocument) { SigningKey = key };
var reference = new Reference { Uri = "" };
reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
xml.AddReference(reference);
xml.ComputeSignature();
return xml.GetXml();
}
private static string xmlDocumentToString(XmlDocument xmlDocument)
{
using (var ms = new MemoryStream())
{
var settings = new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8 };
var xmlWriter = XmlWriter.Create(ms, settings);
xmlDocument.Save(xmlWriter);
ms.Position = 0;
return new StreamReader(ms).ReadToEnd();
}
}
}
}
using System;
using System.IO;
namespace SignedXmlSample
{
class Program
{
static void Main(string[] args)
{
//Console.WriteLine(LicenseGenerator.CreateRSAKeyPair());
writeLicense();
readLicense();
Console.WriteLine("Press a key...");
Console.ReadKey();
}
private static void readLicense()
{
var xml = File.ReadAllText("License.xml");
const string publicKey = @"<RSAKeyValue>
<Modulus>
mBNKFIc/LkMfaXvLlB/+6EujPkx3vBOvLu8jdESDSQLisT8K96RaDMD1ORmdw2XNdMw/6ZBuJjLhoY13qCU9t7biuL3SIxr858oJ1RLM4PKhA/wVDcYnJXmAUuOyxP/vfvb798o6zAC1R2QWuzG+yJQR7bFmbKH0tXF/NOcSgbc=
</Modulus>
<Exponent>
AQAB
</Exponent>
</RSAKeyValue>";
var result = LicenseGenerator.ReadLicense(publicKey, xml);
Console.WriteLine(result.Domain);
Console.WriteLine(result.IssuedTo);
}
private static void writeLicense()
{
const string rsaData = @"<RSAKeyValue>
<Modulus>
mBNKFIc/LkMfaXvLlB/+6EujPkx3vBOvLu8jdESDSQLisT8K96RaDMD1ORmdw2XNdMw/6ZBuJjLhoY13qCU9t7biuL3SIxr858oJ1RLM4PKhA/wVDcYnJXmAUuOyxP/vfvb798o6zAC1R2QWuzG+yJQR7bFmbKH0tXF/NOcSgbc=
</Modulus>
<Exponent>
AQAB
</Exponent>
<P>
xwPKN77EcolMTD2O2Csv6k9Y4aen8UBVYjeQ4PtrNGz0Zx6I1MxLEFzRpiKC/Ney3xKg0Icwj0ebAQ04d5+HAQ==
</P>
<Q>
w568t0Xe6OBUfCyAuo7tTv4eLgczHntVLpjjcxdUksdVw7NJtlnOLApJVJ+U6/85Z7Ji+eVhuN91yn04pQkAtw==
</Q>
<DP>
svkEjRdA4WP5uoKNiHdmMshCvUQh8wKRBq/D2aAgq9fj/yxlj0FdrAxc+ZQFyk5MbPH6ry00jVWu3sY95s4PAQ==
</DP>
<DQ>
WcRsIUYk5oSbAGiDohiYeZlPTBvtr101V669IUFhhAGJL8cEWnOXksodoIGimzGBrD5GARrr3yRcL1GLPuCEvQ==
</DQ>
<InverseQ>
wIbuKBZSCioG6MHdT1jxlv6U1+Y3TX9sHED9PqGzWWpVGA+xFJmQUxoFf/SvHzwbBlXnG0DLqUvxEv+BkEid2w==
</InverseQ>
<D> Yk21yWdT1BfXqlw30NyN7qNWNuM/Uvh2eaRkCrhvFTckSucxs7st6qig2/RPIwwfr6yIc/bE/TRO3huQicTpC2W3aXsBI9822OOX4BdWCec2txXpSkbZW24moXu+OSHfAdYoOEN6ocR7tAGykIqENshRO7HvONJsOE5+1kF2GAE=
</D>
</RSAKeyValue>";
string data = LicenseGenerator.CreateLicense(
rsaData,
new License
{
Id = Guid.NewGuid(),
Domain = "dotnettips.info",
Expiration = DateTime.Now.AddYears(2),
IssuedTo = "VahidN",
Type = LicenseType.Standard
});
File.WriteAllText("License.xml", data);
}
}
}
مقاله کاملا مفید و جامع بود من نمونه مثال را دانلود و اجرا کردم ولی متاسفانه چیزی در مورد طریقه تست برنامه نگفتین؟
چطوری تست کنم تا متوجه بشم مجوز کار میکنه یا نه؟