Exemple #1
0
        public override unsafe bool TryExportPkcs8PrivateKey(
            Span <byte> destination,
            out int bytesWritten)
        {
            ECParameters ecParameters = ExportParameters(true);

            fixed(byte *privPtr = ecParameters.D)
            {
                try
                {
                    using (AsnWriter writer = ecParameters.WritePkcs8PrivateKey())
                    {
                        return(writer.TryEncode(destination, out bytesWritten));
                    }
                }
                finally
                {
                    CryptographicOperations.ZeroMemory(ecParameters.D);
                }
            }
        }
Exemple #2
0
        public override unsafe bool TryExportEncryptedPkcs8PrivateKey(
            ReadOnlySpan <char> password,
            PbeParameters pbeParameters,
            Span <byte> destination,
            out int bytesWritten)
        {
            if (pbeParameters == null)
            {
                throw new ArgumentNullException(nameof(pbeParameters));
            }

            PasswordBasedEncryption.ValidatePbeParameters(
                pbeParameters,
                password,
                ReadOnlySpan <byte> .Empty);

            ECParameters ecParameters = ExportParameters(true);

            fixed(byte *privPtr = ecParameters.D)
            {
                try
                {
                    using (AsnWriter pkcs8PrivateKey = ecParameters.WritePkcs8PrivateKey())
                        using (AsnWriter writer = KeyFormatHelper.WriteEncryptedPkcs8(
                                   password,
                                   pkcs8PrivateKey,
                                   pbeParameters))
                        {
                            return(writer.TryEncode(destination, out bytesWritten));
                        }
                }
                finally
                {
                    CryptographicOperations.ZeroMemory(ecParameters.D);
                }
            }
        }