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

86 lines
2.7 KiB
C#

/*
* 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 License
{
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 = 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);
}
}
}
}