Files
cdrtool/LicenseFile.cs
Doug Macintosh 336b0dbb7e first real commit
2026-03-08 17:05:59 -04:00

159 lines
5.5 KiB
C#

#define PROD_
/*
* Created by SharpDevelop.
* User: Doug.Macintosh
* Date: 2023-02-05
* Time: 9:52 AM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Runtime.ConstrainedExecution;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;
namespace cdrtool
{
class LicenseFile
{
private class Lic
{
public string Company { get; set; }
public string Product { get; set; }
public string Version { get; set; }
public string Type { get; set; }
public string ValidTo { get; set; }
public bool Perpetual { get; set; }
}
#if !PROD_
//LicenseFile.makeLicenseFile(licfile, "Support", 5, false);
public static void makeLicenseFile(string filename, string lictype, int licyears, bool eoy)
{
string secretKey;
string KeyExpiration;
if (eoy)
{
//KeyExpiration = String.Format("{0}-12-31", DateTime.Now.Year + licyears - 1);
//KeyExpiration = String.Format("{0}-12-31", DateTime.Now.Year); // MFA beta 1
KeyExpiration = String.Format("2030-12-31"); // MFA beta 1
}
else
{
KeyExpiration = DateTime.Now.AddYears(licyears).ToString("yyyy-MM-dd");
}
cdrtool.company = "Hydro Ottawa Limited 4596";
Lic mylicense = new Lic
{
Company = cdrtool.company,
Product = cdrtool.product,
Version = cdrtool.version.Substring(0, 3),
Type = lictype,
ValidTo = KeyExpiration,
Perpetual = (licyears >= 5)
//Perpetual = true
};
Console.WriteLine("c:{0} p:{1} v:{2}", cdrtool.company, cdrtool.product, cdrtool.version);
secretKey = _makeHash(cdrtool.company + cdrtool.product + cdrtool.version.Substring(0, 3)).Substring(0, 32);
Console.WriteLine("sk:{0}", secretKey);
string clearText = JsonConvert.SerializeObject(mylicense, Formatting.Indented);
Console.WriteLine(clearText);
string encryptedKey = Encrypt(clearText, secretKey);
File.WriteAllText(filename, encryptedKey);
}
private static string Encrypt(string clearText, string secretKey)
{
byte[] clearTextBytes = Encoding.UTF8.GetBytes(clearText);
using (Aes aes = Aes.Create())
{
aes.Key = Encoding.UTF8.GetBytes(secretKey);
aes.GenerateIV();
using (ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV))
{
byte[] encryptedBytes = encryptor.TransformFinalBlock(clearTextBytes, 0, clearTextBytes.Length);
byte[] combinedBytes = new byte[aes.IV.Length + encryptedBytes.Length];
aes.IV.CopyTo(combinedBytes, 0);
encryptedBytes.CopyTo(combinedBytes, aes.IV.Length);
return Convert.ToBase64String(combinedBytes);
}
}
}
#endif
public static string validate(string filename)
{
string license;
if ((!File.Exists(filename)) || (new FileInfo(filename).Length == 0))
throw new FileNotFoundException("License file missing or corrupt.");
if (cdrtool.company.Length < 6)
throw new InvalidDataException("User account missing or corrupt.");
try
{
license = _getLicense(filename, _makeHash(cdrtool.company + cdrtool.product + cdrtool.version.Substring(0, 3)).Substring(0, 32));
Logger.Log(5, "Decrypted License Key: {0}", license);
}
catch (Exception)
{
throw new InvalidDataException("Unable to decrypt license file, please contact Support.");
}
return license;
}
private static string _getLicense(string filename, string secretKey)
{
return _Decrypt(File.ReadAllText(filename), secretKey);
}
private static string _Decrypt(string encryptedText, string secretKey)
{
byte[] encryptedTextBytes = Convert.FromBase64String(encryptedText);
using (Aes aes = Aes.Create())
{
aes.Key = Encoding.UTF8.GetBytes(secretKey);
byte[] iv = new byte[aes.BlockSize / 8];
byte[] encrypted = new byte[encryptedTextBytes.Length - iv.Length];
Array.Copy(encryptedTextBytes, 0, iv, 0, iv.Length);
Array.Copy(encryptedTextBytes, iv.Length, encrypted, 0, encrypted.Length);
aes.IV = iv;
using (ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
{
byte[] decryptedBytes = decryptor.TransformFinalBlock(encrypted, 0, encrypted.Length);
return Encoding.UTF8.GetString(decryptedBytes);
}
}
}
private static string _makeHash(string InputString)
{
using (SHA256 sha256Hash = SHA256.Create())
{
byte[] bytes = sha256Hash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(InputString));
return Convert.ToBase64String(bytes);
}
}
}
}