Ejemplo n.º 1
0
        /// <summary>
        /// Export RSA private key
        /// </summary>
        /// <param name="rsa"></param>
        /// <param name="type"></param>
        /// <param name="privateKey"></param>
        /// <param name="isPem">Only valid if the private key type is PKCS#1 and PKCS#8.</param>
        /// <returns></returns>
        public static void ImportPrivateKey(this RSA rsa, RSAKeyType type, string privateKey, bool isPem = false)
        {
            if (isPem)
            {
                privateKey = PemFormatUtil.RemoveFormat(privateKey);
            }

            switch (type)
            {
            case RSAKeyType.Pkcs1:
                rsa.ImportRSAPrivateKey(Convert.FromBase64String(privateKey), out _);
                break;

            case RSAKeyType.Pkcs8:
                rsa.ImportPkcs8PrivateKey(Convert.FromBase64String(privateKey), out _);
                break;

            case RSAKeyType.Xml:
                rsa.ImportXmlPrivateKey(privateKey);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Export RSA private key
        /// </summary>
        /// <param name="rsa"></param>
        /// <param name="type"></param>
        /// <param name="usePemFormat">Only valid if the private key type is PKCS#1 and PKCS#8.</param>
        /// <returns></returns>
        public static string ExportPrivateKey(this RSA rsa, RSAKeyType type, bool usePemFormat = false)
        {
            var key = string.Empty;

            switch (type)
            {
            case RSAKeyType.Pkcs1:
                key = Convert.ToBase64String(rsa.ExportRSAPrivateKey());
                break;

            case RSAKeyType.Pkcs8:
                key = Convert.ToBase64String(rsa.ExportPkcs8PrivateKey());
                break;

            case RSAKeyType.Xml:
                key = rsa.ExportXmlPrivateKey();
                break;
            }

            if (usePemFormat && type != RSAKeyType.Xml)
            {
                key = PemFormatUtil.GetPrivateKeyFormat(type, key);
            }

            return(key);
        }
Ejemplo n.º 3
0
        public static byte[] ExportPkcs8PublicKey(this RSA rsa)
        {
            var pub = rsa.ExportParameters(false);
            RsaKeyParameters rsaKeyParameters = new RsaKeyParameters(false, new BigInteger(1, pub.Modulus), new BigInteger(1, pub.Exponent));
            StringWriter     sw   = new StringWriter();
            PemWriter        pWrt = new PemWriter(sw);

            pWrt.WriteObject(rsaKeyParameters);
            pWrt.Writer.Close();
            return(Convert.FromBase64String(PemFormatUtil.RemoveFormat(sw.ToString())));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Export RSA public key
        /// </summary>
        /// <param name="rsa"></param>
        /// <param name="type"></param>
        /// <param name="usePemFormat">Only valid if the public key type is PKCS#1 and PKCS#8.</param>
        /// <returns></returns>
        public static string ExportPublicKey(this RSA rsa, RSAKeyType type, bool usePemFormat = false)
        {
            var key = type switch
            {
                RSAKeyType.Pkcs1 => Convert.ToBase64String(rsa.ExportRSAPublicKey()),
                RSAKeyType.Pkcs8 => Convert.ToBase64String(rsa.ExportPkcs8PublicKey()),
                RSAKeyType.Xml => rsa.ExportXmlPublicKey(),
                _ => string.Empty
            };

            if (usePemFormat && type != RSAKeyType.Xml)
            {
                key = PemFormatUtil.GetPublicKeyFormat(type, key);
            }

            return(key);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Export RSA public key
        /// </summary>
        /// <param name="rsa"></param>
        /// <param name="type"></param>
        /// <param name="publicKey"></param>
        /// <param name="isPem">Only valid if the private key type is PKCS#1 and PKCS#8.</param>
        /// <returns></returns>
        public static void ImportPublicKey(this RSA rsa, RSAKeyType type, string publicKey, bool isPem = false)
        {
            if (isPem)
            {
                publicKey = PemFormatUtil.RemoveFormat(publicKey);
            }

            switch (type)
            {
            case RSAKeyType.Pkcs1:
            case RSAKeyType.Pkcs8:
                rsa.ImportRSAPublicKey(Convert.FromBase64String(publicKey), out _);
                break;

            case RSAKeyType.Xml:
                rsa.ImportXmlPublicKey(publicKey);
                break;
            }
        }