Beispiel #1
0
        public IActionResult OnPostConvertPEM(string xmldata)
        {
            XMLData = xmldata;

            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();

            try
            {
                provider.FromXmlString(xmldata);

                byte[] ber = null;
                if (provider.PublicOnly)
                {
                    ber     = provider.ExportSubjectPublicKeyInfo();
                    PEMData = MakePem(ber, "PUBLIC KEY");
                }
                else
                {
                    ber     = provider.ExportPkcs8PrivateKey();
                    PEMData = MakePem(ber, "PRIVATE KEY");

                    var berRSA = provider.ExportRSAPrivateKey();
                    RSAData = MakePem(berRSA, "RSA PRIVATE KEY");
                }
            }
            catch (Exception ex) {
                ErrorJS = "<script>toastr.error('" + ex.Message.Replace("'", "¡¦") + "')</script>";
            }

            return(Page());
        }
Beispiel #2
0
        private const string XmlPublicAndPrivateKeyPattern  = "(\\s*<\\s*RSAKeyValue\\s*>\\s*(?:\\s*<\\s*Modulus\\s*>\\s*[a-zA-Z0-9\\+\\/]+={0,2}\\s*<\\/\\s*Modulus\\s*>()|\\s*<\\s*Exponent\\s*>\\s*[a-zA-Z0-9\\+\\/]+={0,2}\\s*<\\/\\s*Exponent\\s*>()|\\s*<\\s*P\\s*>\\s*[a-zA-Z0-9\\+\\/]+={0,2}\\s*<\\/\\s*P\\s*>()|\\s*<\\s*Q\\s*>\\s*[a-zA-Z0-9\\+\\/]+={0,2}\\s*<\\/\\s*Q\\s*>()|\\s*<\\s*DP\\s*>\\s*[a-zA-Z0-9\\+\\/]+={0,2}\\s*<\\/\\s*DP\\s*>()|\\s*<\\s*DQ\\s*>\\s*[a-zA-Z0-9\\+\\/]+={0,2}\\s*<\\/\\s*DQ\\s*>()|\\s*<\\s*InverseQ\\s*>\\s*[a-zA-Z0-9\\+\\/]+={0,2}\\s*<\\/\\s*InverseQ\\s*>()|\\s*<\\s*D\\s*>\\s*[a-zA-Z0-9\\+\\/]+={0,2}\\s*<\\/\\s*D\\s*>()){8}\\s*<\\/\\s*RSAKeyValue\\s*>\\s*\\2\\3\\4\\5\\6\\7\\8\\9)|(\\s*<\\s*RSAKeyValue\\s*>\\s*(?:\\s*<\\s*Modulus\\s*>\\s*[a-zA-Z0-9\\+\\/]+={0,2}\\s*<\\/\\s*Modulus\\s*>()|\\s*<\\s*Exponent\\s*>\\s*[a-zA-Z0-9\\+\\/]+={0,2}\\s*<\\/\\s*Exponent\\s*>()){2}\\s*<\\/\\s*RSAKeyValue\\s*>\\s*\\11\\12)";                                                                                                                                                                                                                                                                                    //https://regex101.com/r/fQV2VN/4  (\s*<\s*RSAKeyValue\s*>\s*(?:\s*<\s*Modulus\s*>\s*[a-zA-Z0-9\+\/]+={0,2}\s*<\/\s*Modulus\s*>()|\s*<\s*Exponent\s*>\s*[a-zA-Z0-9\+\/]+={0,2}\s*<\/\s*Exponent\s*>()|\s*<\s*P\s*>\s*[a-zA-Z0-9\+\/]+={0,2}\s*<\/\s*P\s*>()|\s*<\s*Q\s*>\s*[a-zA-Z0-9\+\/]+={0,2}\s*<\/\s*Q\s*>()|\s*<\s*DP\s*>\s*[a-zA-Z0-9\+\/]+={0,2}\s*<\/\s*DP\s*>()|\s*<\s*DQ\s*>\s*[a-zA-Z0-9\+\/]+={0,2}\s*<\/\s*DQ\s*>()|\s*<\s*InverseQ\s*>\s*[a-zA-Z0-9\+\/]+={0,2}\s*<\/\s*InverseQ\s*>()|\s*<\s*D\s*>\s*[a-zA-Z0-9\+\/]+={0,2}\s*<\/\s*D\s*>()){8}\s*<\/\s*RSAKeyValue\s*>\s*\2\3\4\5\6\7\8\9)|(\s*<\s*RSAKeyValue\s*>\s*(?:\s*<\s*Modulus\s*>\s*[a-zA-Z0-9\+\/]+={0,2}\s*<\/\s*Modulus\s*>()|\s*<\s*Exponent\s*>\s*[a-zA-Z0-9\+\/]+={0,2}\s*<\/\s*Exponent\s*>()){2}\s*<\/\s*RSAKeyValue\s*>\s*\11\12)


        public static RsaKeyPair CreateRsaKeyPair(this uint keySize, RsaKeyEncoding encoding = RsaKeyEncoding.Ber, bool indent = true)
        {
            RsaKeyPair result;

            using (RSACryptoServiceProvider Csp = new RSACryptoServiceProvider((int)keySize))
            {
                switch (encoding)
                {
                case RsaKeyEncoding.Xml:
                    result = new RsaKeyPair(
                        publicKey: XDocument.Parse(Csp.ToXmlString(false)).ToString(indent ? SaveOptions.None : SaveOptions.DisableFormatting),
                        privateKey: XDocument.Parse(Csp.ToXmlString(true)).ToString(indent ? SaveOptions.None : SaveOptions.DisableFormatting)
                        );
                    break;

                case RsaKeyEncoding.Json:
                    RSAParameters parameters = Csp.ExportParameters(true);
                    var           pubKey     = new
                    {
                        Modulus  = (parameters.Modulus !).ToBase64(),
                        Exponent = (parameters.Exponent !).ToBase64(),
                    };
                    var privKey = new
                    {
                        Modulus  = (parameters.Modulus !).ToBase64(),
                        Exponent = (parameters.Exponent !).ToBase64(),
                        P        = (parameters.P !).ToBase64(),
                        Q        = (parameters.Q !).ToBase64(),
                        DP       = (parameters.DP !).ToBase64(),
                        DQ       = (parameters.DQ !).ToBase64(),
                        InverseQ = (parameters.InverseQ !).ToBase64(),
                        D        = (parameters.D !).ToBase64()
                    };
                    var options = new JsonSerializerOptions
                    {
                        WriteIndented = indent
                    };
                    result = new RsaKeyPair(JsonSerializer.Serialize(pubKey, options), JsonSerializer.Serialize(privKey, options));
                    break;

                case RsaKeyEncoding.Pem:
                    result = new RsaKeyPair($"{RsaPemPublicKeyInitialString}{Environment.NewLine}{Csp.ExportSubjectPublicKeyInfo().ToBase64(HashExtensions.PemLineBreaksLength)}{Environment.NewLine}{RsaPemPublicKeyFinalString}",
                                            $"{RsaPemPrivateKeyInitialString}{Environment.NewLine}{Csp.ExportPkcs8PrivateKey().ToBase64(HashExtensions.PemLineBreaksLength)}{Environment.NewLine}{RsaPemPrivateKeyFinalString}");
                    break;

                default:
                    result = new RsaKeyPair(publicKey: Csp.ExportSubjectPublicKeyInfo().ToBase64(), privateKey: Csp.ExportPkcs8PrivateKey().ToBase64());
                    break;
                }
                ValidateRsaPublicKey(result.PublicKey);
                ValidateRsaPrivateKey(result.PrivateKey);
                return(result);
            }
        }
Beispiel #3
0
        public static RsaKeyPair CreateKeyPair(uint keySize = 4096, RsaKeyEncoding encoding = RsaKeyEncoding.Ber, bool indent = true)
        {
            using (RSACryptoServiceProvider Csp = new RSACryptoServiceProvider((int)keySize))
            {
                switch (encoding)
                {
                case RsaKeyEncoding.Xml:
                    return(new RsaKeyPair(
                               publicKey: XDocument.Parse(Csp.ToXmlString(false)).ToString(indent ? SaveOptions.None : SaveOptions.DisableFormatting),
                               privateKey: XDocument.Parse(Csp.ToXmlString(true)).ToString(indent ? SaveOptions.None : SaveOptions.DisableFormatting)
                               ));

                case RsaKeyEncoding.Json:
                    RSAParameters parameters = Csp.ExportParameters(true);
                    var           pubKey     = new
                    {
                        Modulus  = HashManager.ToBase64(parameters.Modulus !),
                        Exponent = HashManager.ToBase64(parameters.Exponent !),
                    };
                    var privKey = new
                    {
                        Modulus  = HashManager.ToBase64(parameters.Modulus !),
                        Exponent = HashManager.ToBase64(parameters.Exponent !),
                        P        = HashManager.ToBase64(parameters.P !),
                        Q        = HashManager.ToBase64(parameters.Q !),
                        DP       = HashManager.ToBase64(parameters.DP !),
                        DQ       = HashManager.ToBase64(parameters.DQ !),
                        InverseQ = HashManager.ToBase64(parameters.InverseQ !),
                        D        = HashManager.ToBase64(parameters.D !)
                    };
                    var options = new JsonSerializerOptions
                    {
                        WriteIndented = indent
                    };
                    return(new RsaKeyPair(JsonSerializer.Serialize(pubKey, options), JsonSerializer.Serialize(privKey, options)));

                case RsaKeyEncoding.Pem:
                    return(new RsaKeyPair($"{RsaPemPublicKeyInitialString}\n{HashManager.ToBase64(Csp.ExportSubjectPublicKeyInfo(), HashManager.PemLineBreaksLength)}\n{RsaPemPublicKeyFinalString}",
                                          $"{RsaPemPrivateKeyInitialString}\n{HashManager.ToBase64(Csp.ExportPkcs8PrivateKey(), HashManager.PemLineBreaksLength)}\n{RsaPemPrivateKeyFinalString}"));

                default:
                    return(new RsaKeyPair(publicKey: HashManager.ToBase64(Csp.ExportSubjectPublicKeyInfo()), privateKey: HashManager.ToBase64(Csp.ExportPkcs8PrivateKey())));
                }
            }
        }