示例#1
0
        public static string WritePublicKey(string publicKey)
        {
            if (publicKey.StartsWith("-----BEGIN PUBLIC KEY-----"))
            {
                return(publicKey);
            }
            var akp = RSAUtilities.GetAsymmetricKeyParameterFormPublicKey(publicKey);

            using (var sw = new StringWriter())
            {
                var pWrt = new PemWriter(sw);
                pWrt.WriteObject(akp);
                pWrt.Writer.Close();
                return(sw.ToString());
            }
        }
        /// <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 = RSAUtilities.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()));
            }
        }
示例#3
0
        public static string WritePkcs8PrivateKey(string privateKey)
        {
            if (privateKey.StartsWith("-----BEGIN PRIVATE KEY-----"))
            {
                return(privateKey);
            }

            var akp = RSAUtilities.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());
            }
        }