public static MatrixInt Decrypt(ILinearCode linearCode, IList <int> permutation, IList <int> mask, MatrixInt scramblerMatrix, ParityCheckMatrixGeneratorEllyptic generator, MatrixInt encryptedMessage) { var message = encryptedMessage.Clone(); #region Unmask for (int col = 0; col < message.ColumnCount; col++) { for (int row = 0; row < message.RowCount; row++) { message[row, col] = linearCode.GaloisField.MultiplyWords(message[row, col], linearCode.GaloisField.GetMultiplicativeInverse(mask[col])); } } #endregion Debug.WriteLine(message); #region Inverse permutation var inversePermutation = Helper.InversePermutation(permutation); message = message.PermuteColumns(inversePermutation); #endregion Debug.WriteLine(message); #region Correct Errors var correctedMessage = DecoderEllyptic.DecodeAndCorrect(linearCode, message, generator); #endregion Debug.WriteLine(correctedMessage); #region Apply the inverse scrambler matrix var inverseScramblerMatrix = MatrixAlgorithms.MatrixInverse(scramblerMatrix, linearCode.GaloisField); var decryptedMessage = MatrixAlgorithms.DotMultiplication(correctedMessage, inverseScramblerMatrix, linearCode.GaloisField); #endregion return(decryptedMessage); }
public MatrixInt DecryptMessage(MatrixInt encryptedMessage) { var message = encryptedMessage.Clone(); #region Unmask for (int col = 0; col < message.ColumnCount; col++) { for (int row = 0; row < message.RowCount; row++) { message[row, col] = LinearCode.GaloisField.MultiplyWords(message[row, col], LinearCode.GaloisField.GetMultiplicativeInverse(PrivateKey.Mask[col])); } } #endregion //Debug.WriteLine(message); #region Inverse permutation var inversePermutation = Helper.InversePermutation(PrivateKey.Permutation); message = message.PermuteColumns(inversePermutation); #endregion //Debug.WriteLine(message); #region Correct Errors var correctedMessage = LinearCode.DecodeAndCorrect(message); #endregion //Debug.WriteLine(correctedMessage); #region Apply the inverse scrambler matrix var inverseScramblerMatrix = MatrixAlgorithms.MatrixInverse(PrivateKey.ScramblerMatrix, LinearCode.GaloisField); var decryptedMessage = MatrixAlgorithms.DotMultiplication(correctedMessage, inverseScramblerMatrix, LinearCode.GaloisField); #endregion return(decryptedMessage); }
public McEliece(ILinearCode linearCode, MatrixInt scramblerMatrix, IList <int> permutation, IList <int> mask) { LinearCode = linearCode; PrivateKey = new PrivateKey { GeneratorMatrix = linearCode.GeneratorMatrix, ScramblerMatrix = scramblerMatrix, Permutation = permutation, Mask = mask }; var encryptionMatrix = MatrixAlgorithms.DotMultiplication(scramblerMatrix, linearCode.GeneratorMatrix, linearCode.GaloisField); Debug.WriteLine(encryptionMatrix); encryptionMatrix = encryptionMatrix.PermuteColumns(PrivateKey.Permutation); Debug.WriteLine(encryptionMatrix); for (int col = 0; col < encryptionMatrix.ColumnCount; col++) { for (int row = 0; row < encryptionMatrix.RowCount; row++) { encryptionMatrix[row, col] = linearCode.GaloisField.MultiplyWords(encryptionMatrix[row, col], mask[col]); } } Debug.WriteLine(encryptionMatrix + 1); PublicKey = new PublicKey { EncryptionMatrix = encryptionMatrix, ErrorVectorMaxWeight = linearCode.MinimumDistance }; }
public MatrixInt DecryptMessage(MatrixInt encryptedMessage) { var message = encryptedMessage.Clone(); #region Unmask for (int col = 0; col < message.ColumnCount; col++) { for (int row = 0; row < message.RowCount; row++) { message[row, col] = LinearCode.GaloisField.MultiplyWords(message[row, col], PrivateKey.InverseMask[col]); } } #endregion Debug.WriteLine(message); #region Inverse permutation message = message.PermuteColumns(PrivateKey.InversePermutation); #endregion Debug.WriteLine(message); #region Correct Errors var correctedMessage = DecoderEllyptic.DecodeAndCorrect(LinearCode, message, _generator); #endregion Debug.WriteLine(correctedMessage); #region Apply the inverse scrambler matrix var decryptedMessage = MatrixAlgorithms.DotMultiplication(correctedMessage, PrivateKey.InverseScramblerMatrix, LinearCode.GaloisField); #endregion return(decryptedMessage); }
public MatrixInt EncryptMessage(PublicKey publicKey, MatrixInt message, MatrixInt errorVector) { var encryptedMessage = MatrixAlgorithms.DotMultiplication(message, publicKey.EncryptionMatrix, LinearCode.GaloisField); for (int i = 0; i < encryptedMessage.ColumnCount; i++) { encryptedMessage[0, i] = LinearCode.GaloisField.AddWords(encryptedMessage[0, i], errorVector[0, i]); } return(encryptedMessage); }
public void McElieseGenericFormTest(int n, int k, int d, int t, int fieldPower, MatrixInt message, MatrixInt errorVector, MatrixInt scrambler, int[] permutation, int[] mask) { var galoisField = new GaloisField(2, fieldPower); var generator = new ParityCheckMatrixGeneratorEllyptic(2); var linearCode = new LinearCode(n, k, d, t, galoisField); while (true) { linearCode.ParityCheckMatrix = generator.Generate(linearCode); if (Helper.Weight(linearCode.ParityCheckMatrix) < Math.Ceiling(linearCode.ParityCheckMatrix.RowCount * linearCode.ParityCheckMatrix.ColumnCount * 0.7)) { continue; } try { linearCode.GeneratorMatrix = GeneratorMatrixCalculator.CalculateGeneratorMatrixAlt(linearCode); } catch (LinearCodeException ex) { Console.WriteLine(ex.Message); continue; } if (Helper.Weight(MatrixAlgorithms.DotMultiplication(linearCode.GeneratorMatrix, linearCode.ParityCheckMatrix.Transpose(), galoisField)) == 0) { linearCode.GeneratorMatrix = linearCode.GeneratorMatrix; break; } } var crytptogram = McElieseGenericForm.Encrypt(linearCode, scrambler, permutation, mask, generator, message, errorVector); var decryptedMessage = McElieseGenericForm.Decrypt(linearCode, permutation, mask, scrambler, generator, crytptogram); Assert.True(message == decryptedMessage); }
public static MatrixInt Encrypt(ILinearCode linearCode, MatrixInt scramblerMatrix, IList <int> permutation, IList <int> mask, ParityCheckMatrixGeneratorEllyptic generator, MatrixInt message, MatrixInt errorVector) { var encryptionMatrix = MatrixAlgorithms.DotMultiplication(scramblerMatrix, linearCode.GeneratorMatrix, linearCode.GaloisField); Debug.WriteLine(encryptionMatrix); encryptionMatrix = encryptionMatrix.PermuteColumns(permutation); Debug.WriteLine(encryptionMatrix); for (int col = 0; col < encryptionMatrix.ColumnCount; col++) { for (int row = 0; row < encryptionMatrix.RowCount; row++) { encryptionMatrix[row, col] = linearCode.GaloisField.MultiplyWords(encryptionMatrix[row, col], mask[col]); } } Debug.WriteLine(encryptionMatrix); var encryptedMessage = MatrixAlgorithms.DotMultiplication(message, encryptionMatrix, linearCode.GaloisField); for (int i = 0; i < encryptedMessage.ColumnCount; i++) { encryptedMessage[0, i] = linearCode.GaloisField.AddWords(encryptedMessage[0, i], errorVector[0, i]); } return(encryptedMessage); }
public MatrixInt Encode(MatrixInt message) { return(MatrixAlgorithms.DotMultiplication(message, GeneratorMatrix, GaloisField)); }
private bool IsGeneratorMatrixValid(MatrixInt generatorMatrix, MatrixInt parityCheckMatrix, GaloisField galoisField) { var result = MatrixAlgorithms.DotMultiplication(generatorMatrix, parityCheckMatrix.Transpose(), galoisField); return(Helper.Weight(result) == 0); }
public McElieseEllyptic(int n, int k, int d, int t, GaloisField galoisField, MatrixInt scramblerMatrix = null, IList <int> permutation = null, IList <int> mask = null) { _generator = new ParityCheckMatrixGeneratorEllyptic(2); LinearCode = new LinearCode(n, k, d, t, galoisField); MatrixInt parityCheckMatrix = null; MatrixInt generatorMatrix = null; while (true) { parityCheckMatrix = _generator.Generate(LinearCode); LinearCode.ParityCheckMatrix = parityCheckMatrix; Debug.WriteLine(parityCheckMatrix); var minValueFillPercentage = 0.7; if (Helper.Weight(parityCheckMatrix) < Math.Ceiling(parityCheckMatrix.RowCount * parityCheckMatrix.ColumnCount * minValueFillPercentage)) { continue; } try { generatorMatrix = GeneratorMatrixCalculator.CalculateGeneratorMatrixAlt(LinearCode); Debug.WriteLine(generatorMatrix); } catch (LinearCodeException ex) { Debug.WriteLine(ex.Message); continue; } if (IsGeneratorMatrixValid(parityCheckMatrix, generatorMatrix, galoisField)) { LinearCode.GeneratorMatrix = generatorMatrix; break; } } if (scramblerMatrix is null) { scramblerMatrix = Helper.GenerateScramblerMatrix(LinearCode.K); while (true) { try { MatrixAlgorithms.MatrixInverse(scramblerMatrix, galoisField); break; } catch (SolveMatrixException) { Debug.WriteLine("Reattempting to generate scrambler matrix"); } } } if (permutation is null) { permutation = Helper.GeneratePermutaionList(LinearCode.N); } if (mask is null) { mask = Helper.GenerateMask(LinearCode.N, LinearCode.GaloisField); } var inverseMask = new List <int>(LinearCode.N); for (int i = 0; i < LinearCode.N; i++) { inverseMask.Add(LinearCode.GaloisField.GetMultiplicativeInverse(mask[i])); } PrivateKey = new PrivateKey { GeneratorMatrix = LinearCode.GeneratorMatrix, ScramblerMatrix = scramblerMatrix, InverseScramblerMatrix = MatrixAlgorithms.MatrixInverse(scramblerMatrix, galoisField), Permutation = permutation, InversePermutation = Helper.InversePermutation(permutation), Mask = mask, InverseMask = inverseMask }; var encryptionMatrix = MatrixAlgorithms.DotMultiplication(PrivateKey.ScramblerMatrix, generatorMatrix, LinearCode.GaloisField); Debug.WriteLine(encryptionMatrix); encryptionMatrix = encryptionMatrix.PermuteColumns(PrivateKey.Permutation); Debug.WriteLine(encryptionMatrix); for (int col = 0; col < encryptionMatrix.ColumnCount; col++) { for (int row = 0; row < encryptionMatrix.RowCount; row++) { encryptionMatrix[row, col] = LinearCode.GaloisField.MultiplyWords(encryptionMatrix[row, col], PrivateKey.Mask[col]); } } Debug.WriteLine(encryptionMatrix); PublicKey = new PublicKey { EncryptionMatrix = encryptionMatrix, }; }