Exemplo n.º 1
0
        public int Decode(int input)
        {
            IEnumerable <int> roundKeys = KeyScheduler.GetRoundKeys(RoundCount);

            input = AddRoundKey(input, roundKeys.Last());

            for (int i = roundKeys.Count() - 2; i >= 0; i--)
            {
                // Permutation Layer Except Last Round
                if (i != RoundCount - 1)
                {
                    input = Permutation.InversePermutate(input);
                }

                // SBox Substitution
                byte[] inputBytes = GetBytes(input);
                for (int j = 0; j < 4; j++)
                {
                    inputBytes[j] = Substitution.InverseSubstitute(inputBytes[j]);
                }
                input = GetInt(inputBytes);

                // Add Round Key
                input = AddRoundKey(input, roundKeys.ElementAt(i));
            }

            return(input);
        }
Exemplo n.º 2
0
        private Dictionary <int, double> CalculateOutputDifference(Dictionary <int, int> inputs, List <int> partialKeys)
        {
            Dictionary <int, double> partialKeyMatchProbabilities = new Dictionary <int, double>();

            byte[] inputsOfLastSubBoxes = GetBytes(Characteristic.OutputDifference);

            double correctPairs = 0;

            foreach (KeyValuePair <int, int> inputPair in inputs)
            {
                int    firstOutput         = _spn.Encode(inputPair.Key);
                int    secondOutput        = _spn.Encode(inputPair.Value);
                int    outputDifference    = firstOutput ^ secondOutput;
                byte[] outputsOfDifference = GetBytes(outputDifference);

                bool invalidPair = false;
                for (int i = 0; i < _spn.SubstitutionBoxCount; i++)
                {
                    if (!ActiveSubstitutionBoxesInLastRound.Contains(i))
                    {
                        if (outputsOfDifference[i] != 0)
                        {
                            invalidPair = true;
                            break;
                        }
                    }
                }
                if (invalidPair)
                {
                    continue;
                }

                correctPairs++;
                foreach (int partialKey in partialKeys)
                {
                    int    firstOutputOfLastSubBoxes   = (firstOutput ^ partialKey);
                    int    secondOutputOfLastSubBoxes  = (secondOutput ^ partialKey);
                    byte[] firstOutputsOfLastSubBoxes  = GetBytes(firstOutputOfLastSubBoxes);
                    byte[] secondOutputsOfLastSubBoxes = GetBytes(secondOutputOfLastSubBoxes);

                    bool partialKeyMatched = true;
                    for (int i = 0; i < _spn.SubstitutionBoxCount; i++)
                    {
                        if (ActiveSubstitutionBoxesInLastRound.Contains(i))
                        {
                            firstOutputsOfLastSubBoxes[i]  = Substitution.InverseSubstitute(firstOutputsOfLastSubBoxes[i]);
                            secondOutputsOfLastSubBoxes[i] = Substitution.InverseSubstitute(secondOutputsOfLastSubBoxes[i]);
                            byte difference = (byte)(firstOutputsOfLastSubBoxes[i] ^ secondOutputsOfLastSubBoxes[i]);

                            if (difference != inputsOfLastSubBoxes[i])
                            {
                                partialKeyMatched = false;
                                break;
                            }
                        }
                    }

                    if (partialKeyMatched)
                    {
                        if (!partialKeyMatchProbabilities.ContainsKey(partialKey))
                        {
                            partialKeyMatchProbabilities.Add(partialKey, 0);
                        }
                        partialKeyMatchProbabilities[partialKey]++;
                    }
                }
            }

            partialKeyMatchProbabilities = partialKeyMatchProbabilities.OrderByDescending(p => p.Value).ToDictionary(k => k.Key, v => v.Value / correctPairs);
            return(partialKeyMatchProbabilities);
        }