コード例 #1
0
        /// <summary>
        /// Create and initialize structure from binary data with defined header
        /// </summary>
        /// <exception cref="CryptographicException">On validate errors</exception>
        /// <returns>Initialized structure</returns>
        public static PrivateKeyBlob FromBinary(BinaryReader reader, BlobHeader header)
        {
            var privateKeyBlob = new PrivateKeyBlob
            {
                Header    = header,
                RSAPubKey = RSAPubKey.FromBinary(reader),
            };

            int byteLength = (int)(privateKeyBlob.RSAPubKey.BitLength >> 3);
            int wordLength = (int)(privateKeyBlob.RSAPubKey.BitLength >> 4);

            privateKeyBlob.Modulus = new byte[byteLength];
            reader.Read(privateKeyBlob.Modulus, 0, privateKeyBlob.Modulus.Length);

            privateKeyBlob.Prime1 = new byte[wordLength];
            reader.Read(privateKeyBlob.Prime1, 0, privateKeyBlob.Prime1.Length);

            privateKeyBlob.Prime2 = new byte[wordLength];
            reader.Read(privateKeyBlob.Prime2, 0, privateKeyBlob.Prime2.Length);

            privateKeyBlob.Exponent1 = new byte[wordLength];
            reader.Read(privateKeyBlob.Exponent1, 0, privateKeyBlob.Exponent1.Length);

            privateKeyBlob.Exponent2 = new byte[wordLength];
            reader.Read(privateKeyBlob.Exponent2, 0, privateKeyBlob.Exponent2.Length);

            privateKeyBlob.Coefficient = new byte[wordLength];
            reader.Read(privateKeyBlob.Coefficient, 0, privateKeyBlob.Coefficient.Length);

            privateKeyBlob.PrivateExponent = new byte[byteLength];
            reader.Read(privateKeyBlob.PrivateExponent, 0, privateKeyBlob.PrivateExponent.Length);

            return(privateKeyBlob);
        }
コード例 #2
0
        /// <summary>
        /// Create and initialize structure from RSAParameters
        /// </summary>
        /// <returns>Initialized structure</returns>
        /// <note>http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsaparameters.aspx</note>
        public static PrivateKeyBlob FromRSAParameters(RSAParameters @params)
        {
            var privateKeyBlob = new PrivateKeyBlob
            {
                Header    = BlobHeader.FromRSAParameters(KeyBlobType.PrivateKeyBlob),
                RSAPubKey = RSAPubKey.FromRSAParameters(@params, true),
            };

            privateKeyBlob.Modulus = new byte[@params.N.Length];
            for (int i = 0; i < privateKeyBlob.Modulus.Length; i++)
            {
                privateKeyBlob.Modulus[i] = @params.N[@params.N.Length - i - 1];
            }

            privateKeyBlob.Prime1 = new byte[@params.P.Length];
            for (int i = 0; i < privateKeyBlob.Prime1.Length; i++)
            {
                privateKeyBlob.Prime1[i] = @params.P[@params.P.Length - i - 1];
            }

            privateKeyBlob.Prime2 = new byte[@params.Q.Length];
            for (int i = 0; i < privateKeyBlob.Prime2.Length; i++)
            {
                privateKeyBlob.Prime2[i] = @params.Q[@params.Q.Length - i - 1];
            }

            privateKeyBlob.Exponent1 = new byte[@params.DP.Length];
            for (int i = 0; i < privateKeyBlob.Exponent1.Length; i++)
            {
                privateKeyBlob.Exponent1[i] = @params.DP[@params.DP.Length - i - 1];
            }

            privateKeyBlob.Exponent2 = new byte[@params.DQ.Length];
            for (int i = 0; i < privateKeyBlob.Exponent2.Length; i++)
            {
                privateKeyBlob.Exponent2[i] = @params.DQ[@params.DQ.Length - i - 1];
            }

            privateKeyBlob.Coefficient = new byte[@params.IQ.Length];
            for (int i = 0; i < privateKeyBlob.Coefficient.Length; i++)
            {
                privateKeyBlob.Coefficient[i] = @params.IQ[@params.IQ.Length - i - 1];
            }

            privateKeyBlob.PrivateExponent = new byte[@params.D.Length];
            for (int i = 0; i < privateKeyBlob.PrivateExponent.Length; i++)
            {
                privateKeyBlob.PrivateExponent[i] = @params.D[@params.D.Length - i - 1];
            }

            return(privateKeyBlob);
        }
コード例 #3
0
        /// <summary>
        /// Exports a blob that contains the key information associated with an AsymmetricAlgorithm object.
        /// </summary>
        /// <param name="includePrivateParameters">true to include the private key; otherwise, false.</param>
        /// <returns>A byte array that contains the key information associated with an AsymmetricAlgorithm object</returns>
        public byte[] ExportCspBlob(bool includePrivateParameters)
        {
            var @params = this.ExportParameters();

            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream))
                {
                    if (includePrivateParameters)
                    {
                        PrivateKeyBlob.FromRSAParameters(@params).ToBinary(writer);
                    }
                    else
                    {
                        PublicKeyBlob.FromRSAParameters(@params).ToBinary(writer);
                    }

                    return(stream.ToArray());
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Imports a blob that represents asymmetric key information.
        /// </summary>
        /// <param name="rawData">A byte array that represents an asymmetric key blob.</param>
        /// <exception cref="CryptographicException">Invalid key blob data</exception>
        /// <returns>Initialized RSAParameters structure</returns>
        public void ImportCspBlob(byte[] rawData)
        {
            using (var stream = new MemoryStream(rawData))
            {
                using (var reader = new BinaryReader(stream))
                {
                    BlobHeader header = BlobHeader.FromBinary(reader);

                    if (header.BlobType == KeyBlobType.PublicKeyBlob)
                    {
                        this.ImportParameters(PublicKeyBlob.FromBinary(reader, header).ToRSAParameters());
                        return;
                    }

                    if (header.BlobType == KeyBlobType.PrivateKeyBlob)
                    {
                        this.ImportParameters(PrivateKeyBlob.FromBinary(reader, header).ToRSAParameters());
                        return;
                    }
                }
            }

            throw new CryptographicException("Invalid key blob data");
        }
コード例 #5
0
ファイル: Scrypt.cs プロジェクト: jmaucher/SStack
        /// <summary>
        /// Create and initialize structure from RSAParameters
        /// </summary>
        /// <returns>Initialized structure</returns>
        /// <note>http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsaparameters.aspx</note>
        public static PrivateKeyBlob FromRSAParameters(RSAParameters @params)
        {
            var privateKeyBlob = new PrivateKeyBlob
            {
                Header = BlobHeader.FromRSAParameters(KeyBlobType.PrivateKeyBlob),
                RSAPubKey = RSAPubKey.FromRSAParameters(@params, true),
            };

            privateKeyBlob.Modulus = new byte[@params.N.Length];
            for (int i = 0; i < privateKeyBlob.Modulus.Length; i++)
            {
                privateKeyBlob.Modulus[i] = @params.N[@params.N.Length - i - 1];
            }

            privateKeyBlob.Prime1 = new byte[@params.P.Length];
            for (int i = 0; i < privateKeyBlob.Prime1.Length; i++)
            {
                privateKeyBlob.Prime1[i] = @params.P[@params.P.Length - i - 1];
            }

            privateKeyBlob.Prime2 = new byte[@params.Q.Length];
            for (int i = 0; i < privateKeyBlob.Prime2.Length; i++)
            {
                privateKeyBlob.Prime2[i] = @params.Q[@params.Q.Length - i - 1];
            }

            privateKeyBlob.Exponent1 = new byte[@params.DP.Length];
            for (int i = 0; i < privateKeyBlob.Exponent1.Length; i++)
            {
                privateKeyBlob.Exponent1[i] = @params.DP[@params.DP.Length - i - 1];
            }

            privateKeyBlob.Exponent2 = new byte[@params.DQ.Length];
            for (int i = 0; i < privateKeyBlob.Exponent2.Length; i++)
            {
                privateKeyBlob.Exponent2[i] = @params.DQ[@params.DQ.Length - i - 1];
            }

            privateKeyBlob.Coefficient = new byte[@params.IQ.Length];
            for (int i = 0; i < privateKeyBlob.Coefficient.Length; i++)
            {
                privateKeyBlob.Coefficient[i] = @params.IQ[@params.IQ.Length - i - 1];
            }

            privateKeyBlob.PrivateExponent = new byte[@params.D.Length];
            for (int i = 0; i < privateKeyBlob.PrivateExponent.Length; i++)
            {
                privateKeyBlob.PrivateExponent[i] = @params.D[@params.D.Length - i - 1];
            }

            return privateKeyBlob;
        }
コード例 #6
0
ファイル: Scrypt.cs プロジェクト: jmaucher/SStack
        /// <summary>
        /// Create and initialize structure from binary data with defined header
        /// </summary>
        /// <exception cref="CryptographicException">On validate errors</exception>
        /// <returns>Initialized structure</returns>
        public static PrivateKeyBlob FromBinary(BinaryReader reader, BlobHeader header)
        {
            var privateKeyBlob = new PrivateKeyBlob
            {
                Header = header,
                RSAPubKey = RSAPubKey.FromBinary(reader),
            };

            int byteLength = (int)(privateKeyBlob.RSAPubKey.BitLength >> 3);
            int wordLength = (int)(privateKeyBlob.RSAPubKey.BitLength >> 4);

            privateKeyBlob.Modulus = new byte[byteLength];
            reader.Read(privateKeyBlob.Modulus, 0, privateKeyBlob.Modulus.Length);

            privateKeyBlob.Prime1 = new byte[wordLength];
            reader.Read(privateKeyBlob.Prime1, 0, privateKeyBlob.Prime1.Length);

            privateKeyBlob.Prime2 = new byte[wordLength];
            reader.Read(privateKeyBlob.Prime2, 0, privateKeyBlob.Prime2.Length);

            privateKeyBlob.Exponent1 = new byte[wordLength];
            reader.Read(privateKeyBlob.Exponent1, 0, privateKeyBlob.Exponent1.Length);

            privateKeyBlob.Exponent2 = new byte[wordLength];
            reader.Read(privateKeyBlob.Exponent2, 0, privateKeyBlob.Exponent2.Length);

            privateKeyBlob.Coefficient = new byte[wordLength];
            reader.Read(privateKeyBlob.Coefficient, 0, privateKeyBlob.Coefficient.Length);

            privateKeyBlob.PrivateExponent = new byte[byteLength];
            reader.Read(privateKeyBlob.PrivateExponent, 0, privateKeyBlob.PrivateExponent.Length);

            return privateKeyBlob;
        }