Example #1
0
File: RSA.cs Project: z77ma/runtime
        /// <summary>
        /// Exports the current key in the PKCS#1 RSAPrivateKey format, PEM encoded.
        /// </summary>
        /// <returns>A string containing the PEM-encoded PKCS#1 RSAPrivateKey.</returns>
        /// <exception cref="CryptographicException">
        /// The key could not be exported.
        /// </exception>
        /// <remarks>
        /// <p>
        ///   A PEM-encoded PKCS#1 RSAPrivateKey will begin with <c>-----BEGIN RSA PRIVATE KEY-----</c>
        ///   and end with <c>-----END RSA PRIVATE KEY-----</c>, with the base64 encoded DER
        ///   contents of the key between the PEM boundaries.
        /// </p>
        /// <p>
        ///   The PEM is encoded according to the IETF RFC 7468 &quot;strict&quot;
        ///   encoding rules.
        /// </p>
        /// </remarks>
        public unsafe string ExportRSAPrivateKeyPem()
        {
            byte[] exported = ExportRSAPrivateKey();

            // Fixed to prevent GC moves.
            fixed(byte *pExported = exported)
            {
                try
                {
                    return(PemKeyHelpers.CreatePemFromData(PemLabels.RsaPrivateKey, exported));
                }
                finally
                {
                    CryptographicOperations.ZeroMemory(exported);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Exports the current key in the PKCS#8 EncryptedPrivateKeyInfo format
        /// with a char-based password, PEM encoded.
        /// </summary>
        /// <param name="password">
        /// The password to use when encrypting the key material.
        /// </param>
        /// <param name="pbeParameters">
        /// The password-based encryption (PBE) parameters to use when encrypting the key material.
        /// </param>
        /// <returns>A string containing the PEM-encoded PKCS#8 EncryptedPrivateKeyInfo.</returns>
        /// <exception cref="NotImplementedException">
        /// An implementation for <see cref="ExportEncryptedPkcs8PrivateKey(ReadOnlySpan{char}, PbeParameters)" /> or
        /// <see cref="TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan{char}, PbeParameters, Span{byte}, out int)" /> has not been provided.
        /// </exception>
        /// <exception cref="CryptographicException">
        /// The key could not be exported.
        /// </exception>
        /// <remarks>
        /// <p>
        ///   When <paramref name="pbeParameters" /> indicates an algorithm that
        ///   uses PBKDF2 (Password-Based Key Derivation Function 2), the password
        ///   is converted to bytes via the UTF-8 encoding.
        /// </p>
        /// <p>
        ///   A PEM-encoded PKCS#8 EncryptedPrivateKeyInfo will begin with
        ///  <c>-----BEGIN ENCRYPTED PRIVATE KEY-----</c> and end with
        ///  <c>-----END ENCRYPTED PRIVATE KEY-----</c>, with the base64 encoded DER
        ///   contents of the key between the PEM boundaries.
        /// </p>
        /// <p>
        ///   The PEM is encoded according to the IETF RFC 7468 &quot;strict&quot;
        ///   encoding rules.
        /// </p>
        /// </remarks>
        public unsafe string ExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan <char> password, PbeParameters pbeParameters)
        {
            byte[] exported = ExportEncryptedPkcs8PrivateKey(password, pbeParameters);

            // Fixed to prevent GC moves.
            fixed(byte *pExported = exported)
            {
                try
                {
                    return(PemKeyHelpers.CreatePemFromData(PemLabels.EncryptedPkcs8PrivateKey, exported));
                }
                finally
                {
                    CryptographicOperations.ZeroMemory(exported);
                }
            }
        }
Example #3
0
File: RSA.cs Project: z77ma/runtime
 /// <summary>
 /// Exports the public-key portion of the current key in the PKCS#1
 /// RSAPublicKey format, PEM encoded.
 /// </summary>
 /// <returns>A string containing the PEM-encoded PKCS#1 RSAPublicKey.</returns>
 /// <exception cref="CryptographicException">
 /// The key could not be exported.
 /// </exception>
 /// <remarks>
 /// <p>
 ///   A PEM-encoded PKCS#1 RSAPublicKey will begin with <c>-----BEGIN RSA PUBLIC KEY-----</c>
 ///   and end with <c>-----END RSA PUBLIC KEY-----</c>, with the base64 encoded DER
 ///   contents of the key between the PEM boundaries.
 /// </p>
 /// <p>
 ///   The PEM is encoded according to the IETF RFC 7468 &quot;strict&quot;
 ///   encoding rules.
 /// </p>
 /// </remarks>
 public string ExportRSAPublicKeyPem()
 {
     byte[] exported = ExportRSAPublicKey();
     return(PemKeyHelpers.CreatePemFromData(PemLabels.RsaPublicKey, exported));
 }
Example #4
0
 /// <summary>
 /// Exports the public-key portion of the current key in the X.509
 /// SubjectPublicKeyInfo format, PEM encoded.
 /// </summary>
 /// <returns>A string containing the PEM-encoded X.509 SubjectPublicKeyInfo.</returns>
 /// <exception cref="NotImplementedException">
 /// An implementation for <see cref="ExportSubjectPublicKeyInfo" /> or
 /// <see cref="TryExportSubjectPublicKeyInfo" /> has not been provided.
 /// </exception>
 /// <exception cref="CryptographicException">
 /// The key could not be exported.
 /// </exception>
 /// <remarks>
 /// <p>
 ///   A PEM-encoded X.509 SubjectPublicKeyInfo will begin with
 ///  <c>-----BEGIN PUBLIC KEY-----</c> and end with
 ///  <c>-----END PUBLIC KEY-----</c>, with the base64 encoded DER
 ///   contents of the key between the PEM boundaries.
 /// </p>
 /// <p>
 ///   The PEM is encoded according to the IETF RFC 7468 &quot;strict&quot;
 ///   encoding rules.
 /// </p>
 /// </remarks>
 public string ExportSubjectPublicKeyInfoPem()
 {
     byte[] exported = ExportSubjectPublicKeyInfo();
     return(PemKeyHelpers.CreatePemFromData(PemLabels.SpkiPublicKey, exported));
 }