213 lines
4.8 KiB
C#
213 lines
4.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Xml;
|
|
using System.Security;
|
|
using System.Security.Cryptography;
|
|
using System.Security.Cryptography.Xml;
|
|
|
|
|
|
namespace GroundZero.KeyCodes
|
|
{
|
|
/// <summary>
|
|
/// AyaNova .net key generator
|
|
/// </summary>
|
|
public class KeyGen
|
|
{
|
|
private string _RegisteredTo;
|
|
private int _ScheduleableUsers;
|
|
private bool _FeatureWebInterface;
|
|
System.DateTime _Expires;
|
|
private bool _FeatureQuickBooksInterface;
|
|
|
|
public KeyGen()
|
|
{
|
|
_FeatureWebInterface=false;
|
|
_FeatureQuickBooksInterface=false;
|
|
_Expires=System.DateTime.Today;
|
|
|
|
}
|
|
|
|
public string RegisteredTo
|
|
{
|
|
get
|
|
{
|
|
return _RegisteredTo;
|
|
}
|
|
set
|
|
{
|
|
_RegisteredTo=value;
|
|
}
|
|
}
|
|
|
|
public int ScheduleableUsers
|
|
{
|
|
get
|
|
{
|
|
return _ScheduleableUsers;
|
|
}
|
|
set
|
|
{
|
|
_ScheduleableUsers=value;
|
|
}
|
|
}
|
|
|
|
public bool FeatureWebInterface
|
|
{
|
|
get
|
|
{
|
|
return _FeatureWebInterface;
|
|
}
|
|
set
|
|
{
|
|
_FeatureWebInterface=value;
|
|
}
|
|
}
|
|
|
|
public DateTime Expires
|
|
{
|
|
get
|
|
{
|
|
return _Expires;
|
|
}
|
|
set
|
|
{
|
|
_Expires=value;
|
|
}
|
|
}
|
|
|
|
public bool FeatureQuickBooksInterface
|
|
{
|
|
get
|
|
{
|
|
return _FeatureQuickBooksInterface;
|
|
}
|
|
set
|
|
{
|
|
_FeatureQuickBooksInterface=value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generate keycode based on passed in data
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public string Generate()
|
|
{
|
|
if(_RegisteredTo==null || _RegisteredTo=="")
|
|
throw new ArgumentException("RegisteredTo is required","RegisteredTo");
|
|
|
|
System.Text.StringBuilder sb = new System.Text.StringBuilder();
|
|
|
|
System.Xml.XmlTextWriter w = new System.Xml.XmlTextWriter(new StringWriter(sb));
|
|
w.Formatting = System.Xml.Formatting.Indented;
|
|
|
|
w.WriteStartDocument(true);
|
|
//w.WriteDocType("KeyDocument",null,null,null);
|
|
double dSchemaVersion=1.1;
|
|
w.WriteStartElement("AyaNovaLicenseKey");
|
|
w.WriteElementString("SchemaVersion",XmlConvert.ToString(dSchemaVersion));
|
|
|
|
w.WriteElementString("Created",XmlConvert.ToString(System.DateTime.Now));
|
|
|
|
|
|
|
|
w.WriteElementString("RegisteredTo",_RegisteredTo);
|
|
|
|
|
|
w.WriteElementString("InstallableUntil",XmlConvert.ToString(System.DateTime.Today.AddDays(7)));
|
|
w.WriteElementString("TotalScheduleableUsers",XmlConvert.ToString(_ScheduleableUsers));
|
|
if(!System.DateTime.Today.Date.Equals(_Expires.Date))
|
|
w.WriteElementString("Expires",XmlConvert.ToString(_Expires.Date));
|
|
|
|
|
|
//w.WriteElementString("PermanentKey",XmlConvert.ToString(_TemporaryKey));
|
|
w.WriteElementString("FeatureWebBrowserInterface",XmlConvert.ToString(_FeatureWebInterface));
|
|
w.WriteElementString("FeatureQuickBooksInterface",XmlConvert.ToString(_FeatureQuickBooksInterface));
|
|
|
|
|
|
w.WriteEndElement();
|
|
w.WriteEndDocument();
|
|
|
|
w.Flush();
|
|
w.Close();
|
|
|
|
|
|
// Load the license request file.
|
|
XmlDocument xmldoc = new XmlDocument();
|
|
try
|
|
{
|
|
|
|
xmldoc.LoadXml(sb.ToString());
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
return (ex.Message + ":\r\n\r\n" + sb.ToString());
|
|
}
|
|
|
|
// Get the key pair from the key store.
|
|
|
|
CspParameters parms = new CspParameters(1); // PROV_RSA_FULL
|
|
parms.Flags = CspProviderFlags.UseMachineKeyStore; // Use Machine store
|
|
parms.KeyContainerName = "AyaNovaLicenseContainer"; // "CodeProject" container
|
|
parms.KeyNumber = 2; // AT_SIGNATURE
|
|
try{
|
|
RSACryptoServiceProvider csp = new RSACryptoServiceProvider(parms);
|
|
|
|
// Creating the XML signing object.
|
|
SignedXml sxml = new SignedXml(xmldoc);
|
|
sxml.SigningKey = csp;
|
|
|
|
// Set the canonicalization method for the document.
|
|
sxml.SignedInfo.CanonicalizationMethod =
|
|
SignedXml.XmlDsigCanonicalizationUrl; // No comments.
|
|
|
|
// Create an empty reference (not enveloped) for the XPath
|
|
// transformation.
|
|
Reference r = new Reference("");
|
|
|
|
// Create the XPath transform and add it to the reference list.
|
|
r.AddTransform(new XmlDsigEnvelopedSignatureTransform(false));
|
|
|
|
// Add the reference to the SignedXml object.
|
|
sxml.AddReference(r);
|
|
|
|
// Compute the signature.
|
|
sxml.ComputeSignature();
|
|
|
|
// Get the signature XML and add it to the document element.
|
|
XmlElement sig = sxml.GetXml();
|
|
xmldoc.DocumentElement.AppendChild(sig);
|
|
|
|
// Write-out formatted signed XML to console (allow for redirection).
|
|
System.Text.StringBuilder sbFinal = new System.Text.StringBuilder();
|
|
XmlTextWriter writer = new XmlTextWriter(new StringWriter(sbFinal));
|
|
writer.Formatting = Formatting.Indented;
|
|
|
|
try
|
|
{
|
|
xmldoc.WriteTo(writer);
|
|
}
|
|
finally
|
|
{
|
|
writer.Flush();
|
|
writer.Close();
|
|
}
|
|
|
|
|
|
return sbFinal.ToString();
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
return (ex.Message + ":\r\n\r\n" + sb.ToString());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|