Example #1
0
        public string Decrypt(string text)
        {
            string          encrypted        = null;
            List <BitArray> blocks           = DesHelper.BinaryStringToBitArrayBlocks(text);
            List <BitArray> xoredBlocks      = new List <BitArray>();
            List <BitArray> permutatedBlocks = new List <BitArray>();

            foreach (BitArray bitArray in blocks)
            {
                xoredBlocks.Add(bitArray.Xor(KeyXor1));
            }
            foreach (BitArray bitArray in xoredBlocks)
            {
                permutatedBlocks.Add(Permutations.Permute(Data.InitialPermutationTable1, bitArray));
            }

            foreach (BitArray permutatedBlock in permutatedBlocks)
            {
                DesHelper.DivideToTwoArrays(permutatedBlock, out BitArray leftArray, out BitArray rightArray);

                for (int i = 15; i >= 0; i--)
                {
                    BitArray temp = leftArray;
                    leftArray  = rightArray;
                    rightArray = temp.Xor(FunctionF(rightArray, SubKeys[i]));
                }

                BitArray sum        = leftArray.Add(rightArray);
                BitArray final      = Permutations.Permute(Data.InitialPermutationTable2, sum);
                BitArray xoredFinal = final.Xor(KeyXor2);
                encrypted += xoredFinal.TToString();
            }

            return(encrypted);
        }
Example #2
0
        public void Get16SubKeysFromKey()
        {
            BitArray permutatedKey = Permutations.Permute(Data.Permutation56Table, Key);

            DesHelper.DivideToTwoArrays(permutatedKey, out BitArray leftPermutatedKey, out BitArray rightPermutatedKey);
            List <BitArray> leftShitedKeys  = new List <BitArray>(16);
            List <BitArray> rightShitedKeys = new List <BitArray>(16);

            leftShitedKeys.Add(DesHelper.ShiftLeft(leftPermutatedKey));
            rightShitedKeys.Add(DesHelper.ShiftLeft(rightPermutatedKey));
            for (int i = 0; i < 15; i++)
            {
                leftShitedKeys.Add(DesHelper.ShiftLeft(leftShitedKeys[i]));
                rightShitedKeys.Add(DesHelper.ShiftLeft(rightShitedKeys[i]));
            }

            List <BitArray> shiftedSubKeys = new List <BitArray>(16);

            for (int i = 0; i < 16; i++)
            {
                DesHelper.ConnectTwoArraysIntoOne(leftShitedKeys[i], rightShitedKeys[i], out BitArray connected);
                shiftedSubKeys.Add(connected);
                SubKeys.Add(Permutations.Permute(Data.Permutation48Table, shiftedSubKeys[i]));
            }
        }
Example #3
0
        private BitArray FunctionSBox(BitArray array)
        {
            BitArray sBoxedArray = new BitArray(32);

            for (int i = 0; i < 8; i++)
            {
                BitArray temp = array.GetRange(6 * i, 6);
                sBoxedArray.Add(Permutations.Permute(Data.SBox[i], temp));
            }

            return(sBoxedArray);
        }
Example #4
0
 private BitArray FunctionP(BitArray array)
 {
     return(Permutations.Permute(Data.PermutationPTable, array));
 }
Example #5
0
 private BitArray FunctionE(BitArray rightArray)
 {
     return(Permutations.Permute(Data.PetmutationETable, rightArray));
 }