예제 #1
0
 private static byte[] GenerateValidKey(byte[] keyBytes)
 {
     byte[] ret  = new byte[MaxKeyLength];
     byte[] hash = HashManager.ToRawHash(keyBytes, HashAlgorithm.Sha512);
     Array.Copy(hash, ret, MaxKeyLength);
     return(ret);
 }
예제 #2
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())));
                }
            }
        }
예제 #3
0
        private static void ParsePrivateKey(RSACryptoServiceProvider csp, string privateKey)
        {
            try
            {
                switch (GetKeyEncoding(privateKey))
                {
                case RsaKeyEncoding.Json:
                    JsonDocument jsonDoc = JsonDocument.Parse(privateKey, new JsonDocumentOptions {
                        CommentHandling = JsonCommentHandling.Disallow, MaxDepth = 1
                    });
                    JsonElement jsonRoot   = jsonDoc.RootElement;
                    var         parameters = new RSAParameters();
                    parameters.Modulus  = HashManager.FromBase64(jsonRoot.GetProperty(nameof(parameters.Modulus)).GetString() !.ToString());
                    parameters.Exponent = HashManager.FromBase64(jsonRoot.GetProperty(nameof(parameters.Exponent)).GetString() !.ToString());
                    parameters.P        = HashManager.FromBase64(jsonRoot.GetProperty(nameof(parameters.P)).GetString() !.ToString());
                    parameters.Q        = HashManager.FromBase64(jsonRoot.GetProperty(nameof(parameters.Q)).GetString() !.ToString());
                    parameters.DP       = HashManager.FromBase64(jsonRoot.GetProperty(nameof(parameters.DP)).GetString() !.ToString());
                    parameters.DQ       = HashManager.FromBase64(jsonRoot.GetProperty(nameof(parameters.DQ)).GetString() !.ToString());
                    parameters.InverseQ = HashManager.FromBase64(jsonRoot.GetProperty(nameof(parameters.InverseQ)).GetString() !.ToString());
                    parameters.D        = HashManager.FromBase64(jsonRoot.GetProperty(nameof(parameters.D)).GetString() !.ToString());
                    csp.ImportParameters(parameters);
                    break;

                case RsaKeyEncoding.Xml:
                    csp.FromXmlString(privateKey);
                    break;

                case RsaKeyEncoding.Pem:
                    csp.ImportPkcs8PrivateKey(HashManager.FromBase64(privateKey.Replace(RsaPemPrivateKeyInitialString, string.Empty).Replace(RsaPemPrivateKeyFinalString, string.Empty).Trim()), out int bytesRead1);
                    break;

                default:
                    csp.ImportPkcs8PrivateKey(HashManager.FromBase64(privateKey), out int bytesRead2);
                    break;
                }
            }
            catch (Exception)
            {
                throw new Exception("Unable to parse private key.");
            }
        }
예제 #4
0
 public static string DecryptFromBase64(string data, string key)
 {
     return(HashManager.ToString(DecryptRaw(HashManager.FromBase64(data), HashManager.ToByteArray(key))));
 }
예제 #5
0
 public static string EncryptToBase64(string data, string key)
 {
     return(HashManager.ToBase64(EncryptRaw(HashManager.ToByteArray(data), HashManager.ToByteArray(key))));
 }
예제 #6
0
 public static Boolean VerifyHexSignature(string data, HashAlgorithm hashAlgorithm, string signature, string publicKey)
 {
     return(VerifyRaw(HashManager.ToByteArray(data), hashAlgorithm, HashManager.FromHex(signature), publicKey));
 }
예제 #7
0
 public static string SignHex(string data, HashAlgorithm hashAlgorithm, string privateKey)
 {
     return(HashManager.ToHex(SignRaw(HashManager.ToByteArray(data), hashAlgorithm, privateKey)));
 }
예제 #8
0
 public static string DecryptFromHex(string data, string privateKey)
 {
     return(HashManager.ToString(DecryptRaw(HashManager.FromHex(data), privateKey)));
 }
예제 #9
0
 public static string EncryptToHex(string plainText, string publicKey)
 {
     return(HashManager.ToHex(EncryptRaw(HashManager.ToByteArray(plainText), publicKey)));
 }