class LicenseGenerator
{
public void Generate( string Path, String Name, DateTime StartDate, DateTime UpdateTo)
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml( @"<license>
<Name></Name>
<Date></Date>
<UpdateTo></UpdateTo>
<Signature></Signature>
</license>" );
doc.ChildNodes[0].SelectSingleNode( @"/license/Name" , null ).InnerText = Name;
doc.ChildNodes[0].SelectSingleNode( @"/license/Date" , null ).InnerText = StartDate.ToShortDateString();
doc.ChildNodes[0].SelectSingleNode( @"/license/UpdateTo" , null ).InnerText = UpdateTo.ToShortDateString();
MD5 md5 = new MD5CryptoServiceProvider();
byte [] data = System.Text. Encoding .UTF8.GetBytes(Name + StartDate.ToShortDateString() + UpdateTo.ToShortDateString() + "SomePasswordKey" );
byte [] hash = md5.ComputeHash(data);
doc.ChildNodes[0].SelectSingleNode( @"/license/Signature" , null ).InnerText = Convert .ToBase64String(hash);
doc.Save(System.IO.Path.Combine(Path, "license.xml" ));
}
}
* This source code was highlighted with Source Code Highlighter .
< license >
< Name > </ Name >
< Date > 15.06.2009 </ Date >
< UpdateTo > 15.12.2009 </ UpdateTo >
< Signature > w673nrcuvxjnn7R4heEnvw== </ Signature >
</ license >
* This source code was highlighted with Source Code Highlighter .
public class LicenseVerify
{
public LicenseVerify() { }
public string Name { get ; private set ; }
public DateTime StartDate { get ; private set ;}
public DateTime UpdateTo { get ; private set ;}
public void Verify()
{
string File = "license.xml" ;
if (!System.IO. File .Exists( File ))
{
throw new ApplicationException ( " ! License.xml.\n ." );
}
XmlDocument doc = new XmlDocument ();
doc.Load( File );
string sig1;
string Signature;
try
{
string Name = doc.ChildNodes[0].SelectSingleNode( @"/license/Name" , null ).InnerText;
string StartDate = doc.ChildNodes[0].SelectSingleNode( @"/license/Date" , null ).InnerText;
string UpdateTo = doc.ChildNodes[0].SelectSingleNode( @"/license/UpdateTo" , null ).InnerText;
Signature = doc.ChildNodes[0].SelectSingleNode( @"/license/Signature" , null ).InnerText;
MD5 md5 = new MD5CryptoServiceProvider();
byte [] data = System.Text. Encoding .UTF8.GetBytes(Name + StartDate + UpdateTo + "SomePasswordKey" );
byte [] hash = md5.ComputeHash(data);
sig1 = Convert .ToBase64String(hash);
this .Name = Name;
this .StartDate = Convert .ToDateTime(StartDate);
this .UpdateTo = Convert .ToDateTime(UpdateTo);
}
catch (Exception)
{
throw new ApplicationException( " !\n !\n ." );
}
if (sig1 != Signature)
{
throw new ApplicationException( " !\n !\n ." );
}
if ( DateTime .Now < this .StartDate)
{
throw new ApplicationException( string .Format( " !\n ! {0}\n ." ,StartDate.ToShortDateString()));
}
if (App.Created > this .UpdateTo)
{
throw new ApplicationException( string .Format( " !\n .\n {0}\n ." , UpdateTo.ToShortDateString()));
}
}
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/62315/