private string GetProductKey(string code) { string temp; StringBuilder key; int tCount; key = new StringBuilder(); code = code.Replace("-", ""); temp = AESEncryption.Encrypt(code, ENCRYPTION_KEY); tCount = 0; for (int i = 0; i < temp.Length; i++) { if (Char.IsLetterOrDigit(temp[i])) { key.Append(temp[i].ToString().ToUpper()); tCount++; if (tCount >= 4) { key.Append("-"); tCount = 0; } if (key.Length >= 20) // 4 block of 4 character { break; } } } return(key.ToString().Substring(0, 19)); }
private string GetActivationCode() { string cpuInfo = string.Empty; StringBuilder code = new StringBuilder(); int tCount; ManagementClass mc = new ManagementClass("win32_processor"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if (cpuInfo == "") { //Get only the first CPU's ID cpuInfo = mo.Properties["ProcessorID"].Value.ToString(); break; } } cpuInfo = AESEncryption.Encrypt(cpuInfo, ENCRYPTION_KEY); tCount = 0; for (int i = 0; i < cpuInfo.Length; i++) { if (Char.IsLetterOrDigit(cpuInfo[i])) { code.Append(cpuInfo[i].ToString().ToUpper()); tCount++; if (tCount >= 4) { code.Append("-"); tCount = 0; } if (code.Length >= 20) // 4 block of 4 character { break; } } } return(code.ToString().Substring(0, 19)); }
public bool CheckProductKey() { RegistryKey savedKey; string productKey; string encryptedKey; DateTime insDate; DateTime lwkDate; try { savedKey = Registry.LocalMachine.CreateSubKey(PRODUCT_REGISTRY_KEY); if (savedKey != null) { if (savedKey.GetValue(PRODUCT_KEY_NAME) != null) { productKey = savedKey.GetValue(PRODUCT_KEY_NAME).ToString(); savedKey.Close(); if (productKey != "") { if (activationCode == "") { activationCode = GetActivationCode(); } encryptedKey = GetProductKey(activationCode); if (encryptedKey.Contains(productKey)) { if (encryptedKey.Length == productKey.Length) { return(true); } } } } // product key is not exist or not valid // check for trial period if ((savedKey.GetValue(INSTALLATION_DATE_KEY_NAME) == null) || (savedKey.GetValue(LAST_WORKING_DATE_KEY_NAME) == null)) { // create 2 new sub key for installation date & last working day if (savedKey.GetValue(INSTALLATION_DATE_KEY_NAME) == null) { savedKey.SetValue(INSTALLATION_DATE_KEY_NAME, AESEncryption.Encrypt(DateTime.Now.ToString(), ENCRYPTION_KEY)); } savedKey.SetValue(LAST_WORKING_DATE_KEY_NAME, AESEncryption.Encrypt(DateTime.Now.ToString(), ENCRYPTION_KEY)); } else { insDate = DateTime.Parse(AESEncryption.Decrypt(savedKey.GetValue(INSTALLATION_DATE_KEY_NAME).ToString(), ENCRYPTION_KEY)); lwkDate = DateTime.Parse(AESEncryption.Decrypt(savedKey.GetValue(LAST_WORKING_DATE_KEY_NAME).ToString(), ENCRYPTION_KEY)); if ((DateTime.Now.CompareTo(lwkDate) < 0) || (lwkDate.Subtract(insDate).TotalDays > TRIAL_PERIOD)) { // trial period is over --> show the registration form this.ShowDialog(); return(isRegistered); } savedKey.SetValue(LAST_WORKING_DATE_KEY_NAME, AESEncryption.Encrypt(DateTime.Now.ToString(), ENCRYPTION_KEY)); } } else { // cannot create product key in registry MessageBox.Show("Cannot create product key entry. The program will now abort!"); Environment.Exit(0); } } catch (Exception ex) { // problem occurred when checking product license Console.WriteLine(ex); MessageBox.Show("Problem occurred while checking for product license. The program will now abort!"); Environment.Exit(0); } return(false); }