public static void FromPkcs1PrivateString(this RSA rsa, string privateKey, out RSAParameters parameters)
        {
            privateKey = RSAPemFormatHelper.Pkcs1PrivateKeyFormatRemove(privateKey);
            var pr = new PemReader(new StringReader(privateKey));

            if (!(pr.ReadObject() is AsymmetricCipherKeyPair asymmetricCipherKeyPair))
            {
                throw new Exception("Private key format is incorrect");
            }

            var rsaPrivateCrtKeyParameters = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(
                PrivateKeyInfoFactory.CreatePrivateKeyInfo(asymmetricCipherKeyPair.Private));

            parameters = new RSAParameters
            {
                Modulus  = rsaPrivateCrtKeyParameters.Modulus.ToByteArrayUnsigned(),
                Exponent = rsaPrivateCrtKeyParameters.PublicExponent.ToByteArrayUnsigned(),
                P        = rsaPrivateCrtKeyParameters.P.ToByteArrayUnsigned(),
                Q        = rsaPrivateCrtKeyParameters.Q.ToByteArrayUnsigned(),
                DP       = rsaPrivateCrtKeyParameters.DP.ToByteArrayUnsigned(),
                DQ       = rsaPrivateCrtKeyParameters.DQ.ToByteArrayUnsigned(),
                InverseQ = rsaPrivateCrtKeyParameters.QInv.ToByteArrayUnsigned(),
                D        = rsaPrivateCrtKeyParameters.Exponent.ToByteArrayUnsigned()
            };

            rsa.ImportParameters(parameters);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Import RSA private key
        /// </summary>
        /// <param name="rsa"></param>
        /// <param name="type"></param>
        /// <param name="privateKey"></param>
        /// <param name="isPem"></param>
        public static void ImportPrivateKey(this RSA rsa, RSAKeyTypes type, string privateKey, bool isPem = false)
        {
            if (isPem)
            {
                privateKey = type switch
                {
                    RSAKeyTypes.XML => privateKey,
                    RSAKeyTypes.JSON => privateKey,
                    RSAKeyTypes.Pkcs1 => RSAPemFormatHelper.Pkcs1PrivateKeyFormatRemove(privateKey),
                    RSAKeyTypes.Pkcs8 => RSAPemFormatHelper.Pkcs8PrivateKeyFormatRemove(privateKey),
                    _ => throw new NotSupportedException("Unknown RSA key type.")
                };
            }

            switch (type)
            {
            case RSAKeyTypes.XML:
                rsa.FromLvccXmlString(privateKey);
                break;

            case RSAKeyTypes.JSON:
                rsa.FromJsonString(privateKey);
                break;

            case RSAKeyTypes.Pkcs1:
#if NETCOREAPP3_1 || NETSTANDARD2_1
                rsa.ImportRSAPrivateKey(Convert.FromBase64String(privateKey), out _);
#else
                rsa.FromPkcs1PrivateString(privateKey, out _);
#endif
                break;

            case RSAKeyTypes.Pkcs8:
#if NETCOREAPP3_1 || NETSTANDARD2_1
                rsa.ImportPkcs8PrivateKey(Convert.FromBase64String(privateKey), out _);
#else
                rsa.FromPkcs8PrivateString(privateKey, out _);
#endif
                break;
            }
        }