Exemplo n.º 1
0
        public static string WritePkcs1PrivateKey(string privateKey)
        {
            if (privateKey.StartsWith("-----BEGIN RSA PRIVATE KEY-----"))
            {
                return(privateKey);
            }

            var akp = RSAUtilities.GetAsymmetricKeyParameterFormPrivateKey(privateKey);

            using (var sw = new StringWriter())
            {
                var pWrt = new PemWriter(sw);
                pWrt.WriteObject(akp);
                pWrt.Writer.Close();
                return(sw.ToString());
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Pkcs1>>Pkcs8
        /// </summary>
        /// <param name="privateKey">Pkcs1私钥</param>
        /// <param name="format">是否转PEM格式</param>
        /// <returns></returns>
        public static string PrivateKeyPkcs1ToPkcs8(string privateKey, bool format = false)
        {
            var akp = RSAUtilities.GetAsymmetricKeyParameterFormPrivateKey(privateKey);

            if (format)
            {
                var sw    = new StringWriter();
                var pWrt  = new PemWriter(sw);
                var pkcs8 = new Pkcs8Generator(akp);
                pWrt.WriteObject(pkcs8);
                pWrt.Writer.Close();
                return(sw.ToString());
            }
            else
            {
                var privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(akp);
                return(Base64.ToBase64String(privateKeyInfo.GetEncoded()));
            }
        }