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);
        }
Beispiel #2
0
        /// <summary>
        /// Export RSA public key
        /// </summary>
        /// <param name="rsa"></param>
        /// <param name="type"></param>
        /// <param name="usePemFormat"></param>
        /// <returns></returns>
        public static string ExportPublicKey(this RSA rsa, RSAKeyTypes type, bool usePemFormat = false)
        {
            var key = type switch
            {
                RSAKeyTypes.XML => rsa.ToLvccXmlString(false),
                RSAKeyTypes.JSON => rsa.ToJsonString(false),
#if NETCOREAPP3_1 || NETSTANDARD2_1
                RSAKeyTypes.Pkcs1 => Base64Conv.ToBase64String(rsa.ExportRSAPublicKey()),
                RSAKeyTypes.Pkcs8 => Base64Conv.ToBase64String(rsa.ExportRSAPublicKey()),
#else
                RSAKeyTypes.Pkcs1 => rsa.ToPkcs1PublicString(),
                RSAKeyTypes.Pkcs8 => rsa.ToPkcs8PublicString(),
#endif
                _ => throw new NotSupportedException("Unknown RSA key type.")
            };

            if (usePemFormat)
            {
                key = type switch
                {
                    RSAKeyTypes.XML => key,
                    RSAKeyTypes.JSON => key,
                    RSAKeyTypes.Pkcs1 => RSAPemFormatHelper.Pkcs1PublicKeyFormat(key),
                    RSAKeyTypes.Pkcs8 => RSAPemFormatHelper.Pkcs8PublicKeyFormat(key),
                    _ => throw new NotSupportedException("Unknown RSA key type.")
                };
            }

            return(key);
        }
        // ReSharper disable once IdentifierTypo
        public static void FromPkcs8PublicString(this RSA rsa, string publicKey, out RSAParameters parameters)
        {
            publicKey = RSAPemFormatHelper.Pkcs8PublicKeyFormatRemove(publicKey);
            var publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));

            parameters = new RSAParameters
            {
                Modulus  = publicKeyParam.Modulus.ToByteArrayUnsigned(),
                Exponent = publicKeyParam.Exponent.ToByteArrayUnsigned()
            };

            rsa.ImportParameters(parameters);
        }
        public static void FromPkcs1PublicString(this RSA rsa, string publicKey, out RSAParameters parameters)
        {
            publicKey = RSAPemFormatHelper.Pkcs1PublicKeyFormatRemove(publicKey);
            var pr = new PemReader(new StringReader(publicKey));

            if (!(pr.ReadObject() is RsaKeyParameters rsaKey))
            {
                throw new Exception("Public key format is incorrect");
            }

            parameters = new RSAParameters
            {
                Modulus  = rsaKey.Modulus.ToByteArrayUnsigned(),
                Exponent = rsaKey.Exponent.ToByteArrayUnsigned()
            };

            rsa.ImportParameters(parameters);
        }
        // ReSharper disable once IdentifierTypo
        public static void FromPkcs8PrivateString(this RSA rsa, string privateKey, out RSAParameters parameters)
        {
            privateKey = RSAPemFormatHelper.Pkcs8PrivateKeyFormatRemove(privateKey);
            var privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey));

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

            rsa.ImportParameters(parameters);
        }
Beispiel #6
0
        /// <summary>
        /// Import RSA public key
        /// </summary>
        /// <param name="rsa"></param>
        /// <param name="type"></param>
        /// <param name="publicKey"></param>
        /// <param name="isPem"></param>
        public static void ImportPublicKey(this RSA rsa, RSAKeyTypes type, string publicKey, bool isPem = false)
        {
            if (isPem)
            {
                publicKey = type switch
                {
                    RSAKeyTypes.XML => publicKey,
                    RSAKeyTypes.JSON => publicKey,
                    RSAKeyTypes.Pkcs1 => RSAPemFormatHelper.Pkcs1PublicKeyFormatRemove(publicKey),
                    RSAKeyTypes.Pkcs8 => RSAPemFormatHelper.Pkcs8PublicKeyFormatRemove(publicKey),
                    _ => throw new NotSupportedException("Unknown RSA key type.")
                };
            }

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

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

            case RSAKeyTypes.Pkcs1:
#if NETCOREAPP3_1 || NETSTANDARD2_1
                rsa.ImportRSAPublicKey(Convert.FromBase64String(publicKey), out _);
                break;
#else
                rsa.FromPkcs1PublicString(publicKey, out _);
                break;
#endif

            case RSAKeyTypes.Pkcs8:
#if NETCOREAPP3_1 || NETSTANDARD2_1
                rsa.ImportRSAPublicKey(Convert.FromBase64String(publicKey), out _);
                break;
#else
                rsa.FromPkcs8PublicString(publicKey, out _);
                break;
#endif
            }
        }