Esempio n. 1
0
        /// <summary>Creates and returns an XML string representation of the current <see cref="T:System.Security.Cryptography.DSA" /> object.</summary>
        /// <param name="includePrivateParameters">
        ///       <see langword="true" /> to include private parameters; otherwise, <see langword="false" />. </param>
        /// <returns>An XML string encoding of the current <see cref="T:System.Security.Cryptography.DSA" /> object.</returns>
        // Token: 0x06002179 RID: 8569 RVA: 0x000767D8 File Offset: 0x000749D8
        public override string ToXmlString(bool includePrivateParameters)
        {
            DSAParameters dsaparameters = this.ExportParameters(includePrivateParameters);
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("<DSAKeyValue>");
            stringBuilder.Append("<P>" + Convert.ToBase64String(dsaparameters.P) + "</P>");
            stringBuilder.Append("<Q>" + Convert.ToBase64String(dsaparameters.Q) + "</Q>");
            stringBuilder.Append("<G>" + Convert.ToBase64String(dsaparameters.G) + "</G>");
            stringBuilder.Append("<Y>" + Convert.ToBase64String(dsaparameters.Y) + "</Y>");
            if (dsaparameters.J != null)
            {
                stringBuilder.Append("<J>" + Convert.ToBase64String(dsaparameters.J) + "</J>");
            }
            if (dsaparameters.Seed != null)
            {
                stringBuilder.Append("<Seed>" + Convert.ToBase64String(dsaparameters.Seed) + "</Seed>");
                stringBuilder.Append("<PgenCounter>" + Convert.ToBase64String(Utils.ConvertIntToByteArray(dsaparameters.Counter)) + "</PgenCounter>");
            }
            if (includePrivateParameters)
            {
                stringBuilder.Append("<X>" + Convert.ToBase64String(dsaparameters.X) + "</X>");
            }
            stringBuilder.Append("</DSAKeyValue>");
            return(stringBuilder.ToString());
        }
        /// <summary>用指定长度的指定随机种子生成并返回掩码。</summary>
        /// <returns>长度等于 <paramref name="cbReturn" /> 参数的随机生成的掩码。</returns>
        /// <param name="rgbSeed">用于计算掩码的随机种子。</param>
        /// <param name="cbReturn">生成的掩码的长度(以字节为单位)。</param>
        public override byte[] GenerateMask(byte[] rgbSeed, int cbReturn)
        {
            HashAlgorithm hashAlgorithm = (HashAlgorithm)CryptoConfig.CreateFromName(this.HashNameValue);

            byte[] counter   = new byte[4];
            byte[] numArray  = new byte[cbReturn];
            uint   num       = 0;
            int    dstOffset = 0;

            while (dstOffset < numArray.Length)
            {
                Utils.ConvertIntToByteArray(num++, ref counter);
                hashAlgorithm.TransformBlock(rgbSeed, 0, rgbSeed.Length, rgbSeed, 0);
                hashAlgorithm.TransformFinalBlock(counter, 0, 4);
                byte[] hash = hashAlgorithm.Hash;
                hashAlgorithm.Initialize();
                if (numArray.Length - dstOffset > hash.Length)
                {
                    Buffer.BlockCopy((Array)hash, 0, (Array)numArray, dstOffset, hash.Length);
                }
                else
                {
                    Buffer.BlockCopy((Array)hash, 0, (Array)numArray, dstOffset, numArray.Length - dstOffset);
                }
                dstOffset += hashAlgorithm.Hash.Length;
            }
            return(numArray);
        }
        public override byte[] GenerateMask(byte[] rgbSeed, int cbReturn)
        {
            HashAlgorithm algorithm = (HashAlgorithm)CryptoConfig.CreateFromName(this.HashNameValue);

            byte[] counter = new byte[4];
            byte[] dst     = new byte[cbReturn];
            uint   num     = 0;

            for (int i = 0; i < dst.Length; i += algorithm.Hash.Length)
            {
                Utils.ConvertIntToByteArray(num++, ref counter);
                algorithm.TransformBlock(rgbSeed, 0, rgbSeed.Length, rgbSeed, 0);
                algorithm.TransformFinalBlock(counter, 0, 4);
                byte[] hash = algorithm.Hash;
                algorithm.Initialize();
                if ((dst.Length - i) > hash.Length)
                {
                    Buffer.BlockCopy(hash, 0, dst, i, hash.Length);
                }
                else
                {
                    Buffer.BlockCopy(hash, 0, dst, i, dst.Length - i);
                }
            }
            return(dst);
        }
        /// <summary>Generates and returns a mask from the specified random seed of the specified length.</summary>
        /// <param name="rgbSeed">The random seed to use for computing the mask. </param>
        /// <param name="cbReturn">The length of the generated mask in bytes. </param>
        /// <returns>A randomly generated mask whose length is equal to the <paramref name="cbReturn" /> parameter.</returns>
        // Token: 0x06002244 RID: 8772 RVA: 0x000790F8 File Offset: 0x000772F8
        public override byte[] GenerateMask(byte[] rgbSeed, int cbReturn)
        {
            HashAlgorithm hashAlgorithm = (HashAlgorithm)CryptoConfig.CreateFromName(this.HashNameValue);

            byte[] inputBuffer = new byte[4];
            byte[] array       = new byte[cbReturn];
            uint   num         = 0U;

            for (int i = 0; i < array.Length; i += hashAlgorithm.Hash.Length)
            {
                Utils.ConvertIntToByteArray(num++, ref inputBuffer);
                hashAlgorithm.TransformBlock(rgbSeed, 0, rgbSeed.Length, rgbSeed, 0);
                hashAlgorithm.TransformFinalBlock(inputBuffer, 0, 4);
                byte[] hash = hashAlgorithm.Hash;
                hashAlgorithm.Initialize();
                if (array.Length - i > hash.Length)
                {
                    Buffer.BlockCopy(hash, 0, array, i, hash.Length);
                }
                else
                {
                    Buffer.BlockCopy(hash, 0, array, i, array.Length - i);
                }
            }
            return(array);
        }
Esempio n. 5
0
        // We can provide a default implementation of ToXmlString because we require
        // every DSA implementation to implement ImportParameters
        // If includePrivateParameters is false, this is just an XMLDSIG DSAKeyValue
        // clause.  If includePrivateParameters is true, then we extend DSAKeyValue with
        // the other (private) elements.

        public override String ToXmlString(bool includePrivateParameters)
        {
            // From the XMLDSIG spec, RFC 3075, Section 6.4.1, a DSAKeyValue looks like this:

            /*
             * <element name="DSAKeyValue">
             *   <complexType>
             *     <sequence>
             *       <sequence>
             *         <element name="P" type="ds:CryptoBinary"/>
             *         <element name="Q" type="ds:CryptoBinary"/>
             *         <element name="G" type="ds:CryptoBinary"/>
             *         <element name="Y" type="ds:CryptoBinary"/>
             *         <element name="J" type="ds:CryptoBinary" minOccurs="0"/>
             *       </sequence>
             *       <sequence minOccurs="0">
             *         <element name="Seed" type="ds:CryptoBinary"/>
             *         <element name="PgenCounter" type="ds:CryptoBinary"/>
             *       </sequence>
             *     </sequence>
             *   </complexType>
             * </element>
             */
            // we extend appropriately for private component X
            DSAParameters dsaParams = this.ExportParameters(includePrivateParameters);
            StringBuilder sb        = new StringBuilder();

            sb.Append("<DSAKeyValue>");
            // Add P, Q, G and Y
            sb.Append("<P>" + Convert.ToBase64String(dsaParams.P) + "</P>");
            sb.Append("<Q>" + Convert.ToBase64String(dsaParams.Q) + "</Q>");
            sb.Append("<G>" + Convert.ToBase64String(dsaParams.G) + "</G>");
            sb.Append("<Y>" + Convert.ToBase64String(dsaParams.Y) + "</Y>");
            // Add optional components if present
            if (dsaParams.J != null)
            {
                sb.Append("<J>" + Convert.ToBase64String(dsaParams.J) + "</J>");
            }
            if ((dsaParams.Seed != null))    // note we assume counter is correct if Seed is present
            {
                sb.Append("<Seed>" + Convert.ToBase64String(dsaParams.Seed) + "</Seed>");
                sb.Append("<PgenCounter>" + Convert.ToBase64String(Utils.ConvertIntToByteArray(dsaParams.Counter)) + "</PgenCounter>");
            }

            if (includePrivateParameters)
            {
                // Add the private component
                sb.Append("<X>" + Convert.ToBase64String(dsaParams.X) + "</X>");
            }
            sb.Append("</DSAKeyValue>");
            return(sb.ToString());
        }