Ejemplo n.º 1
0
        private MCTResult <AlgoArrayResponse> Encrypt(IModeBlockCipherParameters param)
        {
            var ivs = TdesPartitionHelpers.SetupIvs(param.Iv);

            var responses = new List <AlgoArrayResponse> {
                new AlgoArrayResponse {
                    IV        = ivs[0],
                    Keys      = param.Key.GetDeepCopy(),
                    PlainText = param.Payload.GetDeepCopy()
                }
            };
            var lastCipherTexts           = new List <BitString>();
            var indexAtWhichToStartSaving = NUMBER_OF_ITERATIONS - NUMBER_OF_OUTPUTS_TO_SAVE;

            for (var i = 0; i < NumberOfCases; i++)
            {
                var encryptionOutputs = new List <BitString>();
                var cipherText        = new BitString(0);
                var encryptionInput   = new BitString(0);
                var key = responses.Last().Keys.GetDeepCopy();
                for (var j = 0; j < NUMBER_OF_ITERATIONS; j++)
                {
                    encryptionInput = j < PARTITIONS ? ivs[j] : encryptionOutputs[j - PARTITIONS];
                    var encryptionOutput = _algo.ProcessPayload(new ModeBlockCipherParameters(
                                                                    BlockCipherDirections.Encrypt, key, encryptionInput
                                                                    )).Result;

                    encryptionOutputs.Add(encryptionOutput.GetDeepCopy());
                    cipherText = param.Payload.XOR(encryptionOutput);

                    if (j >= indexAtWhichToStartSaving)
                    {
                        lastCipherTexts.Insert(0, cipherText.GetDeepCopy());
                    }
                    param.Payload = encryptionInput.GetDeepCopy();
                }

                responses.Last().CipherText = cipherText.GetDeepCopy();

                ivs = TdesPartitionHelpers.SetupIvs(encryptionOutputs[9995].XOR(cipherText));

                responses.Add(new AlgoArrayResponse
                {
                    IV        = ivs[0],
                    Keys      = _keyMaker.MixKeys(new TDESKeys(responses[i].Keys.GetDeepCopy()), lastCipherTexts.ToList()).ToOddParityBitString(),
                    PlainText = responses.Last().PlainText.XOR(encryptionInput)
                });
            }
            responses.RemoveAt(responses.Count() - 1);
            return(new MCTResult <AlgoArrayResponse>(responses));
        }
Ejemplo n.º 2
0
        private MCTResult <AlgoArrayResponse> Encrypt(IModeBlockCipherParameters param)
        {
            if (param.Payload.BitLength != 192)
            {
                throw new ArgumentException("Data must be 192 bits");
            }
            var ivs = TdesPartitionHelpers.SetupIvs(param.Iv);
            var pts = TdesPartitionHelpers.TriPartitionBitString(param.Payload);

            var responses = new List <AlgoArrayResponse> {
                new AlgoArrayResponse {
                    IV        = ivs[0],
                    Keys      = param.Key,
                    PlainText = param.Payload
                }
            };
            var lastCipherTexts           = new List <BitString>();
            var indexAtWhichToStartSaving = NUMBER_OF_ITERATIONS - NUMBER_OF_OUTPUTS_TO_SAVE;

            for (var i = 0; i < NumberOfCases; i++)
            {
                for (var j = 0; j < NUMBER_OF_ITERATIONS; j++)
                {
                    for (var k = 0; k < PARTITIONS; k++)
                    {
                        var result = _algo.ProcessPayload(new ModeBlockCipherParameters(
                                                              BlockCipherDirections.Encrypt,
                                                              responses[i].Keys,
                                                              ivs[k].XOR(pts[k])
                                                              )).Result;
                        pts[k] = ivs[k].GetDeepCopy();
                        ivs[k] = result.GetDeepCopy();
                    }
                    if (j >= indexAtWhichToStartSaving)
                    {
                        lastCipherTexts.Insert(0, ivs[NUMBER_OF_ITERATIONS - 1 - j].GetDeepCopy());
                    }
                }

                responses.Last().CipherText = ivs[0].ConcatenateBits(ivs[1]).ConcatenateBits(ivs[2]);

                responses.Add(new AlgoArrayResponse
                {
                    IV        = ivs[0],
                    Keys      = _keyMaker.MixKeys(new TDESKeys(responses[i].Keys.GetDeepCopy()), lastCipherTexts.ToList()).ToOddParityBitString(),
                    PlainText = pts[0].ConcatenateBits(pts[1].ConcatenateBits(pts[2]))
                });
            }
            responses.RemoveAt(responses.Count() - 1);
            return(new MCTResult <AlgoArrayResponse>(responses));
        }
Ejemplo n.º 3
0
        private Common.Symmetric.MCTResult <AlgoArrayResponse> Encrypt(IModeBlockCipherParameters param)
        {
            var responses = new List <AlgoArrayResponse> {
                new AlgoArrayResponse {
                    IV        = param.Iv,
                    Keys      = param.Key,
                    PlainText = param.Payload
                }
            };
            int numberOfOutputsToSave     = 192 / Shift;
            var indexAtWhichToStartSaving = NUMBER_OF_ITERATIONS - numberOfOutputsToSave;

            for (var i = 0; i < NumberOfCases; i++)
            {
                Debug.WriteLineIf((i + 1) % 10 == 0, $"Running MCT Encryption round {i + 1} out of {NumberOfCases}");
                var       lastResponse     = responses.Last();
                var       tempText         = lastResponse.PlainText.GetDeepCopy();
                var       tempIv           = lastResponse.IV.GetDeepCopy();
                BitString prevTempIv       = null;
                var       lastCipherTexts  = new List <BitString>();
                BitString output           = null;
                var       keysForThisRound = responses[i].Keys;
                for (var j = 0; j < NUMBER_OF_ITERATIONS; j++)
                {
                    prevTempIv = tempIv.GetDeepCopy();
                    output     = _algo.ProcessPayload(new ModeBlockCipherParameters(
                                                          BlockCipherDirections.Encrypt,
                                                          tempIv,
                                                          keysForThisRound,
                                                          tempText
                                                          )).Result;
                    tempText = prevTempIv.MSBSubstring(0, Shift);

                    if (j >= indexAtWhichToStartSaving)
                    {
                        lastCipherTexts.Insert(0, output.GetDeepCopy());
                    }
                }
                lastResponse.CipherText = output;
                responses.Add(new AlgoArrayResponse()
                {
                    Keys      = _keyMaker.MixKeys(new TDESKeys(lastResponse.Keys.GetDeepCopy()), lastCipherTexts).ToOddParityBitString(),
                    PlainText = prevTempIv.GetDeepCopy().MSBSubstring(0, Shift),
                    IV        = tempIv.GetDeepCopy()
                });
            }
            responses.RemoveAt(responses.Count() - 1);
            return(new Common.Symmetric.MCTResult <AlgoArrayResponse>(responses));
        }
Ejemplo n.º 4
0
        private Common.Symmetric.MCTResult <AlgoArrayResponse> Encrypt(IModeBlockCipherParameters param)
        {
            var responses = new List <AlgoArrayResponse> {
                new AlgoArrayResponse {
                    IV        = param.Iv.GetDeepCopy(),
                    Keys      = param.Key.GetDeepCopy(),
                    PlainText = param.Payload.GetDeepCopy()
                }
            };

            var indexAtWhichToStartSaving = NUMBER_OF_ITERATIONS - NUMBER_OF_OUTPUTS_TO_SAVE;

            for (var i = 0; i < NumberOfCases; i++)
            {
                var                   tempText         = responses.Last().PlainText.GetDeepCopy();
                var                   tempIv           = responses.Last().IV.GetDeepCopy();
                BitString             prevTempIv       = null;
                SymmetricCipherResult encryptionResult = null;

                var lastCipherTexts = new List <BitString>();
                for (var j = 0; j < NUMBER_OF_ITERATIONS; j++)
                {
                    prevTempIv       = tempIv.GetDeepCopy();
                    encryptionResult = _algo.ProcessPayload(new ModeBlockCipherParameters(
                                                                BlockCipherDirections.Encrypt,
                                                                tempIv,
                                                                responses[i].Keys,
                                                                tempText
                                                                ));

                    tempText = prevTempIv.GetDeepCopy();
                    if (j >= indexAtWhichToStartSaving)
                    {
                        lastCipherTexts.Insert(0, encryptionResult.Result.GetDeepCopy());
                    }
                }
                responses.Last().CipherText = encryptionResult.Result.GetDeepCopy();

                responses.Add(new AlgoArrayResponse()
                {
                    Keys      = _keyMaker.MixKeys(new TDESKeys(responses.Last().Keys.GetDeepCopy()), lastCipherTexts).ToOddParityBitString(),
                    PlainText = responses.Last().PlainText.GetDeepCopy().XOR(prevTempIv),
                    IV        = tempIv.GetDeepCopy()
                });
            }
            responses.RemoveAt(responses.Count() - 1);
            return(new Common.Symmetric.MCTResult <AlgoArrayResponse>(responses));
        }
Ejemplo n.º 5
0
        private Common.Symmetric.MCTResult <AlgoArrayResponse> Encrypt(IModeBlockCipherParameters param)
        {
            List <AlgoArrayResponse> responses = new List <AlgoArrayResponse>();

            List <BitString> lastCipherTexts = new List <BitString>();

            for (int outerLoop = 0; outerLoop < NumberOfCases; outerLoop++)
            {
                AlgoArrayResponse iIterationResponse = new AlgoArrayResponse()
                {
                    IV        = param.Iv,
                    Keys      = param.Key,
                    PlainText = param.Payload
                };

                BitString jCipherText        = null;
                BitString previousCipherText = null;
                var       ivCopiedBytes      = iIterationResponse.IV.ToBytes();
                param.Iv = new BitString(ivCopiedBytes);

                for (int innerLoop = 0; innerLoop < NUMBER_OF_ITERATIONS; innerLoop++)
                {
                    var jResult = _algo.ProcessPayload(param);
                    jCipherText = jResult.Result;
                    SaveOutputForKeyMixing(jResult.Result.GetDeepCopy(), lastCipherTexts);

                    if (innerLoop == 0)
                    {
                        previousCipherText = iIterationResponse.IV;
                    }

                    param.Payload      = previousCipherText;
                    previousCipherText = jCipherText;
                }

                // Inner loop complete, save response
                iIterationResponse.CipherText = jCipherText;
                responses.Add(iIterationResponse);

                // Setup next loop values
                param.Key =
                    _keyMaker.MixKeys(new TDESKeys(iIterationResponse.Keys.GetDeepCopy()), lastCipherTexts)
                    .ToOddParityBitString();
            }

            return(new Common.Symmetric.MCTResult <AlgoArrayResponse>(responses));
        }
Ejemplo n.º 6
0
        private Common.Symmetric.MCTResult <Common.Symmetric.TDES.AlgoArrayResponse> Encrypt(IModeBlockCipherParameters param)
        {
            var responses = new List <Common.Symmetric.TDES.AlgoArrayResponse>();

            var lastCipherTexts = new List <BitString>();

            for (var outerLoop = 0; outerLoop < NumberOfCases; outerLoop++)
            {
                var iIterationResponse = new Common.Symmetric.TDES.AlgoArrayResponse
                {
                    Keys      = param.Key,
                    PlainText = param.Payload
                };

                BitString jCipherText = null;
                for (var innerLoop = 0; innerLoop < NUMBER_OF_ITERATIONS; innerLoop++)
                {
                    var jResult = _algo.ProcessPayload(param);
                    jCipherText = jResult.Result;
                    SaveOutputForKeyMixing(jResult.Result.GetDeepCopy(), lastCipherTexts);

                    param.Payload = jCipherText;
                }

                // Inner loop complete, save response
                iIterationResponse.CipherText = jCipherText;
                responses.Add(iIterationResponse);

                // Setup next loop values
                param.Key =
                    _keyMaker.MixKeys(new TDESKeys(iIterationResponse.Keys.GetDeepCopy()), lastCipherTexts)
                    .ToOddParityBitString();
            }

            return(new Common.Symmetric.MCTResult <Common.Symmetric.TDES.AlgoArrayResponse>(responses));
        }
Ejemplo n.º 7
0
        public MCTResult <AlgoArrayResponse> Encrypt(IModeBlockCipherParameters param)
        {
            var ivs       = TdesPartitionHelpers.SetupIvs(param.Iv);
            var responses = new List <AlgoArrayResponse> {
                new AlgoArrayResponse {
                    IV        = ivs[0],
                    Keys      = param.Key,
                    PlainText = param.Payload
                }
            };
            var numberOfOutputsToSave     = 192 / Shift;
            var indexAtWhichToStartSaving = NUMBER_OF_ITERATIONS - numberOfOutputsToSave;

            for (var i = 0; i < NumberOfCases; i++)
            {
                ivs = TdesPartitionHelpers.SetupIvs(responses[i].IV.GetDeepCopy());
                var       tempText         = responses[i].PlainText.GetDeepCopy();
                BitString prevTempIv       = null;
                var       tempIv           = responses[i].IV.GetDeepCopy();
                var       keysForThisRound = responses[i].Keys;
                BitString output           = null;
                var       holdouts         = new BitString[3];
                var       lastCipherTexts  = new List <BitString>();
                for (var j = 0; j < NUMBER_OF_ITERATIONS; j++)
                {
                    switch (j)
                    {
                    case 0:
                        tempIv = ivs[0].GetDeepCopy();
                        break;

                    case 1:
                        tempIv = ivs[1].GetDeepCopy();
                        break;

                    case 2:
                        tempIv = ivs[2].GetDeepCopy();
                        break;

                    default:
                        tempIv = prevTempIv.MSBSubstring(Shift, 64 - Shift).ConcatenateBits(holdouts[2]);
                        break;
                    }
                    prevTempIv = tempIv.GetDeepCopy();

                    output = _algo.ProcessPayload(new ModeBlockCipherParameters(
                                                      BlockCipherDirections.Encrypt, tempIv, keysForThisRound, tempText)
                                                  ).Result;

                    holdouts[2] = holdouts[1];
                    holdouts[1] = holdouts[0];
                    holdouts[0] = output;

                    tempText = prevTempIv.MSBSubstring(0, Shift);

                    if (j >= indexAtWhichToStartSaving)
                    {
                        lastCipherTexts.Insert(0, output.GetDeepCopy());
                    }
                }
                responses[i].CipherText = output;

                var newIv  = prevTempIv.MSBSubstring(Shift, 64 - Shift).ConcatenateBits(output);
                var newIvs = TdesPartitionHelpers.SetupIvs(newIv);

                responses.Add(new AlgoArrayResponse()
                {
                    Keys = _keyMaker.MixKeys(new TDESKeys(responses[i].Keys.GetDeepCopy()), lastCipherTexts.ToList())
                           .ToOddParityBitString(),
                    PlainText = prevTempIv.GetDeepCopy().MSBSubstring(0, Shift),
                    IV        = newIvs[0]
                });
            }
            responses.RemoveAt(responses.Count - 1);
            return(new MCTResult <AlgoArrayResponse>(responses));
        }