示例#1
0
        public int Decode(int input)
        {
            IEnumerable <int> roundKeys = KeyScheduler.GetRoundKeys(RoundCount);

            input = AddRoundKey(input, roundKeys.Last());

            for (int i = roundKeys.Count() - 2; i >= 0; i--)
            {
                // Permutation Layer Except Last Round
                if (i != RoundCount - 1)
                {
                    input = Permutation.InversePermutate(input);
                }

                // SBox Substitution
                byte[] inputBytes = GetBytes(input);
                for (int j = 0; j < 4; j++)
                {
                    inputBytes[j] = Substitution.InverseSubstitute(inputBytes[j]);
                }
                input = GetInt(inputBytes);

                // Add Round Key
                input = AddRoundKey(input, roundKeys.ElementAt(i));
            }

            return(input);
        }
示例#2
0
        public void GetNext()
        {
            KeyScheduler a = new KeyScheduler
            {
                Key = key,
                IV  = iv
            };

            for (int i = 32; i <= 100; i++)
            {
                BitArray current = a.GetNext(i);
                CollectionAssert.AreNotEqual(a.GetNext(i), current);
                Assert.AreEqual(current.Length, i);
            }
        }
示例#3
0
        public void IsPseudoRandom()
        {
            KeyScheduler a = new KeyScheduler
            {
                Key = key,
                IV  = iv,
            };
            KeyScheduler b = new KeyScheduler
            {
                Key = key,
                IV  = iv,
            };

            for (int i = 0; i < 100; i++)
            {
                CollectionAssert.AreEqual(a.GetNext(i), b.GetNext(i));
            }
        }
示例#4
0
文件: Generic.cs 项目: Hedzer/Urchin
        private WordEncoder GetNewInstance(int wordSize)
        {
            WordEncoder   encoder = new WordEncoder();
            HashAlgorithm hasher  = new SHA512Managed();

            byte[] key = hasher.ComputeHash(new byte[] { 1, 2, 3 });
            byte[] iv  = hasher.ComputeHash(key);

            KeyScheduler keyScheduler = new KeyScheduler
            {
                Key = key,
                IV  = iv,
            };

            encoder.WordSize = wordSize;
            encoder.Seed     = keyScheduler.GetNext(encoder.SeedSize);
            encoder.Entropy  = new BitArray(hasher.ComputeHash(iv));
            return(encoder);
        }
示例#5
0
        public void Clone()
        {
            KeyScheduler a = new KeyScheduler
            {
                Key = key,
                IV  = iv,
            };

            for (int i = 0; i < 10; i++)
            {
                a.GetNext(i);
            }

            KeyScheduler b = (KeyScheduler)a.Clone();

            for (int i = 0; i < 10; i++)
            {
                CollectionAssert.AreEqual(a.GetNext(i), b.GetNext(i));
            }
        }