public static List <char> CodeWithModuleQTestEncode(int q, List <char> combination, ListBox listBox) { int checkBit = q - (SumOfList(combination, q) % q); checkBit = checkBit == q ? 0 : checkBit; listBox.Items.Add($"Check bit: {q} - ({SumOfList(combination, q)} mod {q}) = {checkBit}"); combination.Add(checkBit < 10 ? checkBit.ToString().ToCharArray()[0] : LettersToNumbers.FirstOrDefault(x => x.Value == checkBit).Key); return(combination); }
public static void IterativeСodeFixMistakes(char[,] inputMatrix, int q, ListBox listBox) { char[,] encodedMatrix = Crypto.IterativeСodeEncode(inputMatrix, q); int columnOfError = 0, rowOfError = 0, checkNumber = 0; for (int i = 0; i < encodedMatrix.GetLength(0) - 2; i++) { if (encodedMatrix[i, encodedMatrix.GetLength(1) - 1] != '0') { rowOfError = i + 1; if (!Int32.TryParse(encodedMatrix[i, encodedMatrix.GetLength(1) - 1].ToString(), out checkNumber)) { checkNumber = LettersToNumbers[encodedMatrix[i, encodedMatrix.GetLength(1) - 1].ToString().ToUpper().ToCharArray()[0]]; } } } for (int i = 0; i < encodedMatrix.GetLength(1) - 2; i++) { if (encodedMatrix[encodedMatrix.GetLength(0) - 1, i] != '0') { columnOfError = i + 1; } } if (columnOfError != 0 && rowOfError != 0) { int falseValue; if (!Int32.TryParse(encodedMatrix[rowOfError - 1, columnOfError - 1].ToString(), out falseValue)) { falseValue = LettersToNumbers[encodedMatrix[rowOfError - 1, columnOfError - 1].ToString().ToUpper().ToCharArray()[0]]; } int realValue = (falseValue + (checkNumber % q)) == q ? 0 : (falseValue + (checkNumber % q)); char realValueChar = realValue <= 10 ? Convert.ToChar(realValue.ToString()) : LettersToNumbers.FirstOrDefault(x => x.Value == (checkNumber % q)).Key; inputMatrix[rowOfError - 1, columnOfError - 1] = realValueChar; listBox.Items.Add( $"Mistake was found in column {columnOfError}, row {rowOfError}: {encodedMatrix[rowOfError - 1, columnOfError - 1]} -> {inputMatrix[rowOfError - 1, columnOfError - 1]}"); listBox.Items.Add("Fixed matrix: "); DisplayMatrixWithLastAdditionalSymbol(listBox, inputMatrix); listBox.Items.Add($"Fixed message: {GetMessageFromMatrix(inputMatrix)}"); } else { listBox.Items.Add("Mistake wasn't found"); } }