Esempio n. 1
0
        /// <summary>
        /// Export a RSA private key as a pem
        /// PKCS#1
        /// </summary>
        /// <param name="rsaCertificate">certificate which contains the private key</param>
        /// <returns>a pem rsa private key export</returns>
        public string PemExportRsaPrivateKey(X509Certificate2 rsaCertificate)
        {
            var rsa = rsaCertificate.GetRSAPrivateKey();

            StringBuilder builder = new StringBuilder();

            builder.AppendLine(PemDecoder.GetBegin(PemTypes.RSA_PRIVATE_KEY));
            builder.AppendLine(Convert.ToBase64String(rsa.ExportRSAPrivateKey(),
                                                      Base64FormattingOptions.InsertLineBreaks));
            builder.AppendLine(PemDecoder.GetEnd(PemTypes.RSA_PRIVATE_KEY));
            return(builder.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// You must use a RSA based certificate for this export to work
        /// PKCS#1
        /// </summary>
        /// <param name="cert"></param>
        /// <returns></returns>
        //public string PemExportRsaPublicKey(X509Certificate2 cert)
        //{
        //    var rsa = cert.GetRSAPublicKey();

        //    StringBuilder builder = new StringBuilder();
        //    builder.AppendLine(PemDecoder.GetBegin(PemTypes.RSA_PUBLIC_KEY));
        //    builder.AppendLine(Convert.ToBase64String(rsa.ExportRSAPublicKey(),
        //            Base64FormattingOptions.InsertLineBreaks));
        //    builder.AppendLine(PemDecoder.GetEnd(PemTypes.RSA_PUBLIC_KEY));
        //    return builder.ToString();
        //}

        /// <summary>
        /// public key certificate export in pem format
        /// </summary>
        /// <param name="certificate"></param>
        /// <returns>CERTIFICATE pem export</returns>
        public string PemExportPublicKeyCertificate(X509Certificate2 certificate)
        {
            var publicKeyCrt = ExportCertificatePublicKey(certificate);
            var deviceVerifyPublicKeyBytes = publicKeyCrt.Export(X509ContentType.Cert);

            StringBuilder builder = new StringBuilder();

            builder.AppendLine(PemDecoder.GetBegin(PemTypes.CERTIFICATE));
            builder.AppendLine(Convert.ToBase64String(deviceVerifyPublicKeyBytes,
                                                      Base64FormattingOptions.InsertLineBreaks));
            builder.AppendLine(PemDecoder.GetEnd(PemTypes.CERTIFICATE));
            return(builder.ToString());
        }
Esempio n. 3
0
        /// <summary>
        /// Exports a certificate as a base64 string in the pem format string
        /// </summary>
        /// <param name="cert">certificate to export</param>
        /// <returns>A pem certificate as a string</returns>
        public string PemExportPfxFullCertificate(X509Certificate2 cert, string password = null)
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendLine(PemDecoder.GetBegin(PemTypes.CERTIFICATE));
            if (string.IsNullOrEmpty(password))
            {
                builder.AppendLine(Convert.ToBase64String(cert.Export(X509ContentType.Pfx),
                                                          Base64FormattingOptions.InsertLineBreaks));
            }
            else
            {
                builder.AppendLine(Convert.ToBase64String(cert.Export(X509ContentType.Pfx, password),
                                                          Base64FormattingOptions.InsertLineBreaks));
            }
            builder.AppendLine(PemDecoder.GetEnd(PemTypes.CERTIFICATE));
            return(builder.ToString());
        }