LicenseManager.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. public class LicenseManager
  5. {
  6. private const string SecretKey = "YourSecretKey"; // 시크릿 키
  7. public static string GenerateLicenseKey(string computerName)
  8. {
  9. using (var sha1 = new SHA1Managed())
  10. {
  11. var inputBytes = Encoding.UTF8.GetBytes(computerName + SecretKey);
  12. var hashBytes = sha1.ComputeHash(inputBytes);
  13. return BitConverter.ToString(hashBytes).Replace("-", "");
  14. }
  15. }
  16. public static bool ValidateLicenseKey(string computerName, string licenseKey)
  17. {
  18. return licenseKey == GenerateLicenseKey(computerName);
  19. }
  20. }
  21. class Program
  22. {
  23. static void Main()
  24. {
  25. var computerName = Environment.MachineName; // 컴퓨터 이름 가져오기
  26. var licenseKey = LicenseManager.GenerateLicenseKey(computerName);
  27. Console.WriteLine($"Generated license key for {computerName}: {licenseKey}");
  28. // Validate the license key
  29. var isValid = LicenseManager.ValidateLicenseKey(computerName, licenseKey);
  30. Console.WriteLine($"License key is valid: {isValid}");
  31. }
  32. }
  33. <?xml version="1.0" encoding="utf-8"?>
  34. <License>
  35. <UserName>JohnDoe</UserName>
  36. <ExpirationDate>2024-12-31</ExpirationDate>
  37. <!-- 기타 라이센스 정보 필드 추가 -->
  38. </License>
  39. using System;
  40. using System.IO;
  41. using System.Xml.Linq;
  42. public class LicenseManager
  43. {
  44. private const string LicenseFilePath = "license.xml"; // 라이센스 파일 경로
  45. public static bool ValidateLicense()
  46. {
  47. try
  48. {
  49. var xml = XDocument.Load(LicenseFilePath);
  50. var userName = xml.Root.Element("UserName")?.Value;
  51. var expirationDateStr = xml.Root.Element("ExpirationDate")?.Value;
  52. if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(expirationDateStr))
  53. {
  54. Console.WriteLine("Invalid license format.");
  55. return false;
  56. }
  57. if (!DateTime.TryParse(expirationDateStr, out var expirationDate))
  58. {
  59. Console.WriteLine("Invalid expiration date format.");
  60. return false;
  61. }
  62. // 여기에서 라이센스 검증 로직을 추가하세요
  63. // 예: 유효 기간, 사용자 정보, 기타 조건 확인
  64. return true; // 유효한 라이센스인 경우
  65. }
  66. catch (Exception ex)
  67. {
  68. Console.WriteLine($"Error validating license: {ex.Message}");
  69. return false; // 라이센스 검증 실패
  70. }
  71. }
  72. }
  73. class Program
  74. {
  75. static void Main()
  76. {
  77. if (LicenseManager.ValidateLicense())
  78. {
  79. Console.WriteLine("License is valid. Starting the program...");
  80. // 프로그램 기동 로직 추가
  81. }
  82. else
  83. {
  84. Console.WriteLine("Invalid license. Program will not start.");
  85. // 프로그램 정지 로직 추가
  86. }
  87. }
  88. }