Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PublicKey" /> class
        /// using SubjectPublicKeyInfo from an <see cref="AsymmetricAlgorithm" />.
        /// </summary>
        /// <param name="key">
        /// An asymmetric algorithm to obtain the SubjectPublicKeyInfo from.
        /// </param>
        /// <exception cref="CryptographicException">
        /// The SubjectPublicKeyInfo could not be decoded. The
        /// <see cref="AsymmetricAlgorithm.ExportSubjectPublicKeyInfo" /> must return a
        /// valid ASN.1-DER encoded X.509 SubjectPublicKeyInfo.
        /// </exception>
        /// <exception cref="NotImplementedException">
        /// <see cref="AsymmetricAlgorithm.ExportSubjectPublicKeyInfo" /> has not been overridden
        /// in a derived class.
        /// </exception>
        public PublicKey(AsymmetricAlgorithm key)
        {
            byte[] subjectPublicKey = key.ExportSubjectPublicKeyInfo();
            DecodeSubjectPublicKeyInfo(
                subjectPublicKey,
                out Oid localOid,
                out AsnEncodedData localParameters,
                out AsnEncodedData localKeyValue);

            _oid = localOid;
            EncodedParameters = localParameters;
            EncodedKeyValue   = localKeyValue;

            // Do not assign _key = key. Otherwise, the public Key property
            // will start returning non Rsa / Dsa types.
        }