예제 #1
0
        /// <summary>
        /// Creates a new PassphrasePrng from a passphrase and salt,
        /// and seeds it with the output of PBKDF2
        /// </summary>
        ///
        /// <param name="Digest">Digest engine</param>
        /// <param name="Passphrase">The passphrase</param>
        /// <param name="Salt">The salt value</param>
        /// <param name="Iterations">The number of transformation iterations performed by the digest with PBKDF2 (default is 10,000)</param>
        /// <param name="DisposeEngine">Dispose of digest engine when <see cref="Dispose()"/> on this class is called (default is true)</param>
        ///
        /// <exception cref="CryptoRandomException">Thrown if a null Digest, Passphrase or Salt are used</exception>
        public PBPRng(IDigest Digest, byte[] Passphrase, byte[] Salt, int Iterations = PKCS_ITERATIONS, bool DisposeEngine = true)
        {
            if (Digest == null)
            {
                throw new CryptoRandomException("PBPRng:Ctor", "Digest can not be null!", new ArgumentNullException());
            }
            if (Passphrase == null)
            {
                throw new CryptoRandomException("PBPRng:Ctor", "Passphrase can not be null!", new ArgumentNullException());
            }
            if (Salt == null)
            {
                throw new CryptoRandomException("PBPRng:Ctor", "Salt can not be null!", new ArgumentNullException());
            }

            try
            {
                m_disposeEngine = DisposeEngine;
                PBKDF2 pkcs = new PBKDF2(Digest, Iterations, false);
                m_digest = Digest;
                pkcs.Initialize(Salt, Passphrase);
                m_rndData = new byte[m_digest.BlockSize];
                pkcs.Generate(m_rndData);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }

            m_position = 0;
        }
예제 #2
0
        private void PKCSTest(int Size, int Iterations, byte[] Salt, byte[] Key, byte[] Output)
        {
            byte[] outBytes = new byte[Size];

            using (PBKDF2 gen = new PBKDF2(new SHA256(), Iterations))
            {
                gen.Initialize(Key, Salt);
                gen.Generate(outBytes, 0, Size);
            }

            if (Evaluate.AreEqual(outBytes, Output) == false)
            {
                throw new Exception("PBKDF2: Values are not equal! Expected: " + HexConverter.ToString(Output) + " Received: " + HexConverter.ToString(outBytes));
            }

            using (PBKDF2 gen = new PBKDF2(new HMAC(new SHA256()), Iterations))
            {
                gen.Initialize(Key, Salt);
                gen.Generate(outBytes, 0, Size);
            }

            if (Evaluate.AreEqual(outBytes, Output) == false)
            {
                throw new Exception("PBKDF2: Values are not equal! Expected: " + HexConverter.ToString(Output) + " Received: " + HexConverter.ToString(outBytes));
            }
        }
예제 #3
0
        /// <summary>
        /// Outputs expected values for the PBKDF2
        /// </summary>
        public string GetPBKDFVector(IDigest Engine, int Rounds = 100)
        {
            int    keySize = Engine.BlockSize;
            PBKDF2 pbk     = new PBKDF2(Engine, Rounds);

            byte[] salt   = new byte[keySize];
            byte[] output = new byte[1024];

            for (int i = 0; i < salt.Length; i++)
            {
                salt[i] = (byte)i;
            }

            pbk.Initialize(salt);
            pbk.Generate(output);

            while (output.Length > 32)
            {
                output = Reduce(output);
            }

            return(HexConverter.ToString(output));
        }