예제 #1
0
        private byte[] Process(BitBlock[] subKeys, byte[] next8ByteBlock)
        {
            BitBlock next64BitBlock = new BitBlock(next8ByteBlock);

            BitBlock initialPermutation_Output = InitialPermutation(next64BitBlock);

            BitBlock leftSide = initialPermutation_Output.LeftSide();

            BitBlock rightSide = initialPermutation_Output.RightSide();

            // 16 Rounds of Encryption/Decryption
            for (int i = 0; i <= 15; i++)
            {
                BitBlock F_Output = F(rightSide, subKeys[i]);

                BitBlock tempRightSide = leftSide ^ F_Output;

                leftSide  = rightSide;
                rightSide = tempRightSide;
            }

            BitBlock finalPermutation_Output = FinalPermutation(rightSide + leftSide);

            byte[] byteArray = BitBlock.ConvertToByteArray(finalPermutation_Output);

            return(byteArray);
        }
예제 #2
0
        private BitBlock[] GenerateSubKeys(ProcessType process, BitBlock masterKey)
        {
            BitBlock PC1_Output = PC1(masterKey);

            BitBlock cBlock = PC1_Output.LeftSide();
            BitBlock dBlock = PC1_Output.RightSide();

            BitBlock[] subKeys = new BitBlock[16];

            switch (process)
            {
            case ProcessType.Encrypt:

                #region Encrypt

                for (int round = 1; round <= 16; round++)
                {
                    if (round == 1 || round == 2 || round == 9 || round == 16)
                    {
                        cBlock = cBlock << 1;
                        dBlock = dBlock << 1;
                    }
                    else
                    {
                        cBlock = cBlock << 2;
                        dBlock = dBlock << 2;
                    }

                    BitBlock key = PC2(cBlock + dBlock);

                    subKeys[round - 1] = key;
                }
                break;

                #endregion

            case ProcessType.Decrypt:

                #region Decrypt

                for (int round = 1; round <= 16; round++)
                {
                    if (round == 1)
                    {
                        // No Rotation
                    }
                    else if (round == 2 || round == 9 || round == 16)
                    {
                        cBlock = cBlock >> 1;
                        dBlock = dBlock >> 1;
                    }
                    else
                    {
                        cBlock = cBlock >> 2;
                        dBlock = dBlock >> 2;
                    }

                    BitBlock key = PC2(cBlock + dBlock);

                    subKeys[round - 1] = key;
                }

                break;

                #endregion
            }

            return(subKeys);
        }