using System.Security.Cryptography;
using System.Text.RegularExpressions;
public enum LicenseValidationResult
public class LicenseManager
private const string PublicKey = "<RSAKeyValue><Modulus>MIIBIjANB...</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
private const string ValidServerResponse = "VALID";
private const string InvalidServerResponse = "INVALID";
private static readonly ILogger _logger = new LoggerConfiguration()
.WriteTo.File("license_validation.log")
public static bool CheckLicenseFormat(string licenseKey)
if (string.IsNullOrEmpty(licenseKey))
_logger.Error("License key cannot be empty.");
string pattern = @"^[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}$";
if (!Regex.IsMatch(licenseKey, pattern))
_logger.Error("Invalid license key format.");
public static bool ValidateLicense(string licenseKey, string signature)
if (string.IsNullOrEmpty(licenseKey) || string.IsNullOrEmpty(signature))
_logger.Error("License key and signature cannot be empty.");
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(PublicKey);
byte[] data = Encoding.UTF8.GetBytes(licenseKey);
byte[] signatureBytes = Convert.FromBase64String(signature);
return rsa.VerifyData(data, CryptoConfig.MapNameToOID("SHA256"), signatureBytes);
_logger.Error(ex, "Validation error");
public static LicenseValidationResult ValidateLicenseWithServer(string licenseKey)
string serverResponse = SimulateServerValidation(licenseKey);
if (serverResponse == ValidServerResponse)
return LicenseValidationResult.Valid;
else if (serverResponse == "EXPIRED")
_logger.Error("License has expired.");
return LicenseValidationResult.Expired;
else if (serverResponse == "REVOKED")
_logger.Error("License has been revoked.");
return LicenseValidationResult.Revoked;
_logger.Error("Invalid server response.");
return LicenseValidationResult.InvalidSignature;
_logger.Error(ex, "Server validation error");
return LicenseValidationResult.ServerError;
private static string SimulateServerValidation(string licenseKey)
return ValidServerResponse;
public static void Main()
string licenseKey = "ABCD-EFGH-IJKL-MNOP";
string signature = "InvalidBase64Signature==";
if (!CheckLicenseFormat(licenseKey))
_logger.Error("Invalid license key format.");
if (!ValidateLicense(licenseKey, signature))
_logger.Error("License validation failed.");
LicenseValidationResult result = ValidateLicenseWithServer(licenseKey);
case LicenseValidationResult.Valid:
_logger.Information("License successfully validated. Welcome!");
case LicenseValidationResult.InvalidFormat:
_logger.Error("Invalid license key format.");
case LicenseValidationResult.InvalidSignature:
_logger.Error("License validation failed.");
case LicenseValidationResult.Expired:
_logger.Error("License has expired.");
case LicenseValidationResult.Revoked:
_logger.Error("License has been revoked.");
case LicenseValidationResult.ServerError:
_logger.Error("Server validation error.");