Beispiel #1
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())));
                }
            }
        }
Beispiel #2
0
 public static string EncryptToBase64(string data, string key)
 {
     return(HashManager.ToBase64(EncryptRaw(HashManager.ToByteArray(data), HashManager.ToByteArray(key))));
 }
Beispiel #3
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 string SignBase64(string data, HashAlgorithm hashAlgorithm, string privateKey)
 {
     return(HashManager.ToBase64(SignRaw(HashManager.ToByteArray(data), hashAlgorithm, privateKey)));
 }