Ejemplo n.º 1
0
        /// <summary>
        /// Pkcs8>>Pkcs1
        /// </summary>
        /// <param name="privateKey">Pkcs8私钥</param>
        /// <param name="format">是否转PEM格式</param>
        /// <returns></returns>
        public static string PrivateKeyPkcs8ToPkcs1(string privateKey, bool format = false)
        {
            var akp = AsymmetricKeyUtilities.GetAsymmetricKeyParameterFormAsn1PrivateKey(privateKey);

            if (format)
            {
                var sw   = new StringWriter();
                var pWrt = new PemWriter(sw);
                pWrt.WriteObject(akp);
                pWrt.Writer.Close();
                return(sw.ToString());
            }
            else
            {
                var privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(akp);
                return(Base64.ToBase64String(privateKeyInfo.ParsePrivateKey().GetEncoded()));
            }
        }
Ejemplo n.º 2
0
        public static string WritePkcs8PrivateKey(string privateKey)
        {
            if (privateKey.StartsWith("-----BEGIN PRIVATE KEY-----"))
            {
                return(privateKey);
            }

            var akp = AsymmetricKeyUtilities.GetAsymmetricKeyParameterFormAsn1PrivateKey(privateKey);

            using (var sw = new StringWriter())
            {
                var pWrt  = new PemWriter(sw);
                var pkcs8 = new Pkcs8Generator(akp);
                pWrt.WriteObject(pkcs8);
                pWrt.Writer.Close();
                return(sw.ToString());
            }
        }