Exemplo n.º 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);
        }
Exemplo n.º 2
0
        private BigInteger GenerateRandomPrime(int desiredSize)
        {
            // Create empty blocks for our prime
            BitBlock potentialPrime_asBitBlock = new BitBlock(desiredSize);

            BigInteger potentialPrime;

            do
            {
                // Convert potential prime to byte array
                byte[] potentialPrime_asByteArray = BitBlock.ConvertToByteArray(potentialPrime_asBitBlock);

                // Fill potential prime with random bits
                cryptoServiceProvider.GetBytes(potentialPrime_asByteArray);

                // Convert potential prime to Big Integers
                potentialPrime = new BigInteger(potentialPrime_asByteArray);

                // If we created negative potential prime, change to positive
                if (potentialPrime.Sign == -1)
                {
                    potentialPrime *= -1;
                }
            } while (!isPrime(potentialPrime, securityToken));

            return(potentialPrime);
        }
Exemplo n.º 3
0
        private BigInteger GenerateP(int desiredSize)
        {
            // Create 2 empty blocks for p and q
            BitBlock p_asBitBlock = new BitBlock(desiredSize);

            BigInteger p = 0;

            bool isPPrime = false;

            do
            {
                // Convert p to byte array
                byte[] p_asByteArray = BitBlock.ConvertToByteArray(p_asBitBlock);

                // Fill p with random bits
                cryptoServiceProvider.GetBytes(p_asByteArray);

                // Convert p to Big Integer
                p = new BigInteger(p_asByteArray);

                // If we created negative p/q, change to positive
                if (p.Sign == -1)
                {
                    p *= -1;
                }

                // Primality Test
                switch (this.primalityTestToUse)
                {
                case PrimalityTest.Fermat:
                    isPPrime = Helpers.FermatPrimalityTest(p, this.securityToken);
                    break;

                case PrimalityTest.RabinMiller:
                    isPPrime = Helpers.MillerRabinPrimalityTest(p, this.securityToken);
                    break;
                }
            } while (!isPPrime);

            return(p);
        }