public BitArray[] Generate(BitArray key)
        {
            if (key.Count != 10)
            {
                throw new ArgumentException("Key length should be 10 bits");
            }

            var p10 = SimplifiedDesHelper.P10(key);

            var first5Bits  = SimplifiedDesHelper.GetBits(p10, 0, 5);
            var second5Bits = SimplifiedDesHelper.GetBits(p10, 5, 5);


            var first5BitsShifted  = SimplifiedDesHelper.LeftShift(first5Bits, 1);
            var second5BitsShifted = SimplifiedDesHelper.LeftShift(second5Bits, 1);


            var mergedKey   = first5BitsShifted.Merge(second5BitsShifted);
            var firstSubKey = SimplifiedDesHelper.P8(mergedKey);


            var a = SimplifiedDesHelper.LeftShift(first5Bits, 3);
            var b = SimplifiedDesHelper.LeftShift(second5Bits, 3);


            var mergedKeyAgain = a.Merge(b);
            var secondSubKey   = SimplifiedDesHelper.P8(mergedKeyAgain);


            return(new[] { firstSubKey, secondSubKey });
        }
Esempio n. 2
0
        private BitArray F(BitArray bitArray, BitArray key)
        {
            if (bitArray.Count != 4)
            {
                throw new ArgumentException("F argument length should be 4 bits");
            }

            var permuteAndExpand = new BitArray(8)
            {
                [0] = bitArray[3],
                [1] = bitArray[0],
                [2] = bitArray[1],
                [3] = bitArray[2],
                [4] = bitArray[1],
                [5] = bitArray[2],
                [6] = bitArray[3],
                [7] = bitArray[0]
            };
            var xoredWithKey = permuteAndExpand.NormalXor(key);

            var leftPart  = SimplifiedDesHelper.GetLeftPart(xoredWithKey);
            var rightPart = SimplifiedDesHelper.GetRightPart(xoredWithKey);

            var leftPartSboxed  = Sbox(leftPart, 1);
            var rightPartSboxed = Sbox(rightPart, 2);

            return(SimplifiedDesHelper.P4(leftPartSboxed.Merge(rightPartSboxed)));
        }
Esempio n. 3
0
        private BitArray Switch(BitArray bitArray)
        {
            var leftPart  = SimplifiedDesHelper.GetLeftPart(bitArray);
            var rightPart = SimplifiedDesHelper.GetRightPart(bitArray);

            return(rightPart.Merge(leftPart));
        }
Esempio n. 4
0
        private BitArray Fk(BitArray afterIp, BitArray key)
        {
            var left  = SimplifiedDesHelper.GetLeftPart(afterIp);
            var right = SimplifiedDesHelper.GetRightPart(afterIp);

            left = left.NormalXor(F(right, key));
            return(left.Merge(right));
        }