コード例 #1
0
        protected override AsymmetricAlgorithm LoadKey(ReadOnlyMemory <byte> pkcs8)
        {
            PrivateKeyInfoAsn   privateKeyInfo = PrivateKeyInfoAsn.Decode(pkcs8, AsnEncodingRules.BER);
            AsymmetricAlgorithm key;

            switch (privateKeyInfo.PrivateKeyAlgorithm.Algorithm)
            {
            case Oids.Rsa:
                key = new RSAImplementation.RSASecurityTransforms();
                break;

            case Oids.EcDiffieHellman:
            case Oids.EcPublicKey:
                key = new ECDsaImplementation.ECDsaSecurityTransforms();
                break;

            default:
                throw new CryptographicException(
                          SR.Cryptography_UnknownAlgorithmIdentifier,
                          privateKeyInfo.PrivateKeyAlgorithm.Algorithm);
            }

            key.ImportPkcs8PrivateKey(pkcs8.Span, out int bytesRead);

            if (bytesRead != pkcs8.Length)
            {
                throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
            }

            return(key);
        }
コード例 #2
0
ファイル: CertificatePal.cs プロジェクト: stickybun/corefx
        public ICertificatePal CopyWithPrivateKey(RSA privateKey)
        {
            var typedKey = privateKey as RSAImplementation.RSASecurityTransforms;

            if (typedKey != null)
            {
                return(CopyWithPrivateKey(typedKey.GetKeys()));
            }

            RSAParameters rsaParameters = privateKey.ExportParameters(true);

            using (PinAndClear.Track(rsaParameters.D))
                using (PinAndClear.Track(rsaParameters.P))
                    using (PinAndClear.Track(rsaParameters.Q))
                        using (PinAndClear.Track(rsaParameters.DP))
                            using (PinAndClear.Track(rsaParameters.DQ))
                                using (PinAndClear.Track(rsaParameters.InverseQ))
                                    using (typedKey = new RSAImplementation.RSASecurityTransforms())
                                    {
                                        typedKey.ImportParameters(rsaParameters);
                                        return(CopyWithPrivateKey(typedKey.GetKeys()));
                                    }
        }