using Microsoft.Win32; using System; using System.IO; using System.Linq; using Thermo.Active.Model; namespace Thermo.Active.Utils { public static class CandiesController { private const String BASE36 = "4K6IJZL1M27PQRSNOVWXTU80FGH39ABCDEY5"; private const String BASE10 = "0431786295"; private const String KEY_NAME = "HKEY_LOCAL_MACHINE\\SOFTWARE\\CMS\\ACTIVE"; private const String KEY_VALUE_NAME = "cil"; private const int BASE_TIME = 730119; // 1/1/2000 private static int randomTime = 0; private static TimeSpan MorningTime = new TimeSpan(6, 15, 0); private static DateTime StartTimePlusDelay = DateTime.Now; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region PUBLIC_METHODS // Decodifica password esportando i dati. Ereditata da CMS-Control public static bool LincensePasswordDecode(String Password, out string Matricola, out int MatricolaNumerica, out int Command, out long Parameter) { String decryptedString; int nChk = 0; Matricola = ""; MatricolaNumerica = 0; Command = 0; Parameter = 0; try { decryptedString = AlgoritmoService_Back(Password).ToString(); for (int i = 0; i <= decryptedString.Length - 1 - 1; i++) { nChk += (int)char.GetNumericValue(decryptedString[i]); } //inserita password con checksum errato if (decryptedString.Last() != nChk.ToString().Last()) return false; int indexParameterOffset = 6; if (decryptedString.Count() > 13) { // If decrypted string has more than 13 characters means that the machine number contains a letter var machinNumberString = decryptedString.Substring(1, 11); MatricolaNumerica = int.Parse(machinNumberString); // Convert machineNumber from int to ascii Matricola = SupportFunctions.ConvertIntToAsciiString(MatricolaNumerica); // Change the offset of the parameter indexParameterOffset = 12; } else { // If decrypted string has less than 13 characters use the old method Matricola = decryptedString.Substring(2, 4); MatricolaNumerica = Int32.Parse(Matricola); } Command = short.Parse(decryptedString.Substring(0, 1)); Parameter = long.Parse(decryptedString.Substring(indexParameterOffset, decryptedString.Length - indexParameterOffset - 1)); } catch (Exception) { return false; } return true; } // Lettura della licenza da registro. Ereditata da CMS-Control public static bool GetPCLincense(bool containsLetters, out long EndLincence, out int Matricola, out long Parameter) { string szLicReg_X = String.Empty; long Lincence = 0; Matricola = 0; Parameter = 0; EndLincence = 0; //Leggo nel Registro Licenza_Registry_RW(false, ref szLicReg_X); Lincence = long.Parse(szLicReg_X); return GetDataFromLincense(Lincence, containsLetters, out EndLincence, out Matricola, out Parameter); } // Scrittura della licenza nel registro. Ereditata da CMS-Control public static void SetPCLincense(int Matricola, long Parameter) { string Licenza = SetLincenseFromData(Matricola, Parameter); Licenza_Registry_RW(true, ref Licenza); } // Gestisce la creazione della licenza, da scrivere poi nel CN public static string SetLincenseFromData(int Matricola, long Parameter) { long nLic; int days; if (Parameter > BASE_TIME) days = (int)(Parameter - BASE_TIME); else if (Parameter > Int32.MaxValue) days = Int32.MaxValue; else days = 0; nLic = ((long)Matricola << 16) + days; return AlgoritmoLicence(nLic); } // Gestisce l'export dei dati partendo dalla licenza letta dal CN public static bool GetDataFromLincense(long RawLincence, bool containsLetters, out long EndLincence, out int Matricola, out long Parameter) { Matricola = 0; Parameter = 0; EndLincence = 0; if (RawLincence == 0) return false; EndLincence = AlgoritmoLicence_Back(RawLincence.ToString()); Parameter = EndLincence & 0xFFFF; if (containsLetters) Matricola = (int)((EndLincence >> 16) & 0xFFFFFFFF); else Matricola = (int)((EndLincence >> 16) & 0xFFFF); if (Matricola > 0) return true; else return false; } //Elabora la licenza e decide se va riscritta public static void ElaborateLincense(bool bPC_VALID, bool bNC_VALID, long PCParam, long NCParam, long PcPLic, long NCLic, out DateTime newDate, out bool toRewrite) { long newParam = 0; if (bNC_VALID & bPC_VALID) { if (PCParam == NCParam) { newParam = PCParam + BASE_TIME; toRewrite = false; } else if (PCParam < NCParam) { newParam = PCParam + BASE_TIME; toRewrite = true; } else { newParam = NCParam + BASE_TIME; toRewrite = true; } } else if (bNC_VALID) { newParam = NCParam + BASE_TIME; toRewrite = true; } else if (bPC_VALID) { newParam = PCParam + BASE_TIME; toRewrite = true; } else { // tutto a zero? immagino sia la prima accensione if (PcPLic == 0 && NCLic == 0) { newParam = BASE_TIME; toRewrite = true; } //ho creato il file? immagino sia la prima accensione con memorie cn sporche else if (NCLic == 0 && File.Exists(Constants.CANDY_DUMMYFILE_PATH)) { newParam = BASE_TIME; toRewrite = true; } //a questo punto blocco.. ipotizzo una forzatura else { newParam = 0; toRewrite = true; } } newDate = new DateTime(newParam * TimeSpan.TicksPerDay); } // Controllo dell'orario per iniziare a gestire la licenza public static bool IsTimeToMangeCandies() { //Controllo solo se sono dopo le 6 e 15 + T random (CMS-Control) if (DateTime.Now.TimeOfDay < MorningTime) { randomTime = 0; return false; } // Aggiungo un T random in cui inizio il controllo: da 10 minuti a entro 4 ore dall'avvio if (randomTime == 0) { Random rand = new Random(); randomTime = rand.Next((4 * 60) - 10) + 10; // randomTime = 1; StartTimePlusDelay = DateTime.Now; StartTimePlusDelay = StartTimePlusDelay.Add(new TimeSpan(0, randomTime, 0)); } // If time expired return true if (DateTime.Now > StartTimePlusDelay) return true; //return true; return false; } // Controllo dell'orario per iniziare a controllare public static bool ElaborateExpiredBit(DateTime date, bool NcExpiredBit, out bool NewExpiredBit) { DateTime LastWriteTime; NewExpiredBit = false; //Manage Dummy file if (File.Exists(Constants.CANDY_DUMMYFILE_PATH)) { if (File.GetLastWriteTime(Constants.CANDY_DUMMYFILE_PATH) < DateTime.Now) File.SetLastWriteTime(Constants.CANDY_DUMMYFILE_PATH, DateTime.Now); } else File.Create(Constants.CANDY_DUMMYFILE_PATH); LastWriteTime = File.GetLastWriteTime(Constants.CANDY_DUMMYFILE_PATH); //Manage Lincese Expired Bit //1. If unlimited if (date.Ticks / TimeSpan.TicksPerDay == BASE_TIME) { if (NcExpiredBit) { NewExpiredBit = false; return true; } } //2. If limited else { DateTime SevenDaysDate = DateTime.Now + new TimeSpan(7 * TimeSpan.TicksPerDay); //2.1 today > Expired Lincense Date //2.2 Expired File Date > Expired Lincense Date //2.3 today + 1 Week > Expired File Date if (date < DateTime.Now || date < LastWriteTime || SevenDaysDate < LastWriteTime) { if (!NcExpiredBit) { NewExpiredBit = true; return true; } } else { if (NcExpiredBit) { NewExpiredBit = false; return true; } } } //Nothing to write return false; } // Decodifica password delle teste. Ereditata da CMS-Control public static bool HeadsPasswordCheck(String Password, int Matricola, int Testa) { string[] aCript = new string[19]; string szDay, szMonth, szYear, szRes, psw; long nDay, nMonth, nYear, nAdd1; long nMatr, nHead, nAdd2, nRes; aCript[0] = "Q"; aCript[1] = "W"; aCript[2] = "E"; aCript[3] = "R"; aCript[4] = "Z"; aCript[5] = "Y"; aCript[6] = "U"; aCript[7] = "I"; aCript[8] = "P"; aCript[9] = "A"; aCript[10] = "S"; aCript[11] = "D"; aCript[12] = "F"; aCript[13] = "G"; aCript[14] = "X"; aCript[15] = "J"; aCript[16] = "K"; aCript[17] = "L"; aCript[18] = "M"; szDay = DateTime.Now.Day.ToString(); szMonth = DateTime.Now.Month.ToString(); szYear = DateTime.Now.ToString("yy"); if (Matricola > 0 && Testa >= 0) { //First step calculate the DAY nDay = 11 + Convert.ToInt32(szDay) * 3; nMonth = 12 + Convert.ToInt32(szMonth) * 7; nYear = 31 + Convert.ToInt32(szYear); nAdd1 = 111 + nDay * nMonth * nYear; //Second step calculate the Machine and the head nMatr = (Matricola + 121211); nHead = 131 + (Testa * 17); nAdd2 = nMatr + nHead; //Thirth step mix result .... nRes = (nAdd1 + nAdd2) * 13L; szRes = String.Format(nRes.ToString(), "{0:0000000}"); //Generate Password psw = aCript[Convert.ToInt32(Mid(szRes, 1, 1)) + Convert.ToInt32(Mid(szRes, 7, 1))] + Mid(szRes, 1, 3) + aCript[Convert.ToInt32(Mid(szRes, 6, 1)) + Convert.ToInt32(Mid(szRes, 5, 1))] + Mid(szRes, 4, 3) + aCript[Convert.ToInt32(Mid(szRes, 4, 1)) + Convert.ToInt32(Mid(szRes, 3, 1))] + Mid(szRes, 7, szRes.Length - 6); if (Password == psw) return true; else return false; } return false; } #endregion /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region PRIVATE_METHODS private static long AlgoritmoService_Back(string val) { // Base36ToDec long lVal = 0; int ChrPos; const int BASE = 36; val = val.ToUpper(); while (val.Length > 0) { lVal = lVal * BASE; ChrPos = BASE36.IndexOf(Left(val, 1)); if (ChrPos >= 0) lVal = lVal + ChrPos; val = Mid(val, 2); } return lVal; } private static long AlgoritmoLicence_Back(string val) { long lVal = 0; int ChrPos; const int BASE = 10; val = val.ToUpper(); while (val.Length > 0) { lVal = lVal * BASE; ChrPos = BASE10.IndexOf(Left(val, 1)); if (ChrPos >= 0) lVal = lVal + ChrPos; val = Mid(val, 2); } return lVal; } private static string AlgoritmoLicence(long lVal) { String sVal = ""; while (lVal >= 10) { sVal = sVal + AlgoritmoLicence(lVal / 10); lVal = lVal - 10 * (lVal / 10); } // lVal < 10 sVal = sVal + Mid(BASE10, (int)(lVal + 1), 1); return sVal; } private static string Left(string value, int size) { if (string.IsNullOrEmpty(value)) return value; size = (size >= value.Length ? value.Length : size); string newValue = value.Substring(0, size); return newValue; } private static string Mid(string value, int index) { // Check index and string correctness if (string.IsNullOrEmpty(value)) return value; index = index - 1; if (index >= value.Length) return ""; // Create substring return value.Substring(index); } private static string Mid(string value, int index, int lenght) { value = Mid(value, index); if (lenght <= value.Length) value = value.Substring(0, lenght); return value; } private static void Licenza_Registry_RW(bool bWrite, ref string Licenza) { Object objLicence; // KEY_NAME if (bWrite) Registry.SetValue(KEY_NAME, KEY_VALUE_NAME, Licenza, RegistryValueKind.String); objLicence = Registry.GetValue(KEY_NAME, KEY_VALUE_NAME, 0); if (objLicence == null) objLicence = 0; Licenza = objLicence.ToString(); } #endregion } }