From 5ad09394e373e2fa4eab6f2e8758808aa2de3f08 Mon Sep 17 00:00:00 2001 From: Lucio Maranta Date: Fri, 14 Feb 2020 11:12:16 +0100 Subject: [PATCH] Fix time parameter with new machine number refactoring --- Step.NC/NcAdapter.cs | 8 ++----- Step.UI/Dialogs/PasswordForm.cs | 8 +++---- Step.Utils/CandiesController.cs | 39 +++++++++++++++++++-------------- Step.Utils/supportFunctions.cs | 24 ++++++++++++++++---- 4 files changed, 48 insertions(+), 31 deletions(-) diff --git a/Step.NC/NcAdapter.cs b/Step.NC/NcAdapter.cs index 66276f90..a9d3ce9d 100644 --- a/Step.NC/NcAdapter.cs +++ b/Step.NC/NcAdapter.cs @@ -1274,17 +1274,13 @@ namespace Step.NC // Check if machine number contains letters bool containsLetters = Regex.IsMatch(strMachNumber, @".*?[a-zA-Z].*?"); int machNumber = 0; + if (containsLetters) - { // Convert ASCII string to a single INT - for (int i = 1; i <= strMachNumber.Count(); i++) - machNumber += strMachNumber[strMachNumber.Count() - i] << (8 * (i - 1)); - } + machNumber = SupportFunctions.ConvertAsciiStringToInt(strMachNumber); else - { // Convert string of digits to a INT machNumber = int.Parse(strMachNumber); - } // Read Data from NC & elaborate it long NcCandy = 0; diff --git a/Step.UI/Dialogs/PasswordForm.cs b/Step.UI/Dialogs/PasswordForm.cs index 65aba8b6..c0301706 100644 --- a/Step.UI/Dialogs/PasswordForm.cs +++ b/Step.UI/Dialogs/PasswordForm.cs @@ -64,10 +64,10 @@ namespace Step.UI if (psw == "cmsserviceonly") bValid = true; - else if (Utils.CandiesController.LincensePasswordDecode(psw, out string Matr, out int Command, out long Parameter)) + else if (Utils.CandiesController.LincensePasswordDecode(psw, out string MatricolaString, out int MatricolaNumerica, out int Command, out long Parameter)) //Controllo la matricola - if (this.Matricola != Matr.ToString()) + if (this.Matricola != MatricolaString) bValid = false; else { @@ -81,7 +81,7 @@ namespace Step.UI if(Nc != null) { Licenza = new DateTime(Parameter * TimeSpan.TicksPerDay); - Nc.WriteCandy(Licenza, Int32.Parse(Matricola)); + Nc.WriteCandy(Licenza, MatricolaNumerica); bValid = false; } break; @@ -91,7 +91,7 @@ namespace Step.UI if (Nc != null) { Licenza = new DateTime(0); - Nc.WriteCandy(Licenza, Int32.Parse(Matricola)); + Nc.WriteCandy(Licenza, MatricolaNumerica); bValid = false; } break; diff --git a/Step.Utils/CandiesController.cs b/Step.Utils/CandiesController.cs index a325a703..f86ee01a 100644 --- a/Step.Utils/CandiesController.cs +++ b/Step.Utils/CandiesController.cs @@ -26,51 +26,56 @@ namespace Step.Utils #region PUBLIC_METHODS // Decodifica password esportando i dati. Ereditata da CMS-Control - public static bool LincensePasswordDecode(String Password, out string Matricola, out int Command, out long Parameter) + public static bool LincensePasswordDecode(String Password, out string Matricola, out int MatricolaNumerica, out int Command, out long Parameter) { - String szVal; + String decryptedString; int nChk = 0; Matricola = ""; + MatricolaNumerica = 0; Command = 0; Parameter = 0; try { - szVal = AlgoritmoService_Back(Password).ToString(); + decryptedString = AlgoritmoService_Back(Password).ToString(); - for (int i = 0; i <= szVal.Length - 1 - 1; i++) + for (int i = 0; i <= decryptedString.Length - 1 - 1; i++) { - nChk += (int)char.GetNumericValue(szVal[i]); + nChk += (int)char.GetNumericValue(decryptedString[i]); } //inserita password con checksum errato - if (szVal.Last() != nChk.ToString().Last()) + if (decryptedString.Last() != nChk.ToString().Last()) return false; - if(szVal.Count() > 13) + int indexParameterOffset = 6; + + if(decryptedString.Count() > 13) { - var machinNumberString = szVal.Substring(1, 11); + // If decrypted string has more than 13 characters means that the machine number contains a letter + var machinNumberString = decryptedString.Substring(1, 11); - int intValue = Int32.Parse(machinNumberString); - - for (int i = 3; i >= 0; i--) - { - Matricola += (char)((byte)(intValue >> (8 * i))); - } + MatricolaNumerica = int.Parse(machinNumberString); + // Convert machineNumber from int to ascii + Matricola = SupportFunctions.ConvertIntToAsciiString(MatricolaNumerica); + // Change the offset of the parameter + indexParameterOffset = 12; } else { - Matricola = szVal.Substring(1, 5); + Matricola = decryptedString.Substring(1, 5); + MatricolaNumerica = Int32.Parse(Matricola); } - Command = Int16.Parse(szVal.Substring(0, 1)); - Parameter = long.Parse(szVal.Substring(6, szVal.Length - 6 - 1)); + Command = short.Parse(decryptedString.Substring(0, 1)); + Parameter = long.Parse(decryptedString.Substring(indexParameterOffset, decryptedString.Length - indexParameterOffset - 1)); } catch (Exception) { return false; } + return true; } diff --git a/Step.Utils/supportFunctions.cs b/Step.Utils/supportFunctions.cs index f2a2e9a6..14d7eacd 100644 --- a/Step.Utils/supportFunctions.cs +++ b/Step.Utils/supportFunctions.cs @@ -473,6 +473,26 @@ namespace Step.Utils return true; } + public static string ConvertIntToAsciiString(int integer) + { + string outputString = ""; + for (int i = 3; i >= 0; i--) + { + outputString += (char)((byte)(integer >> (8 * i))); + } + + return outputString; + } + + public static int ConvertAsciiStringToInt(string asciiString) + { + int number = 0; + for (int i = 1; i <= asciiString.Count(); i++) + number += asciiString[asciiString.Count() - i] << (8 * (i - 1)); + + return number; + } + //----- Chipher Private Class --------------------------------- #region Chipher_Private_Class @@ -574,8 +594,4 @@ namespace Step.Utils #endregion } - - - - } \ No newline at end of file