Exemple #1
0
        private void buttonDecode_Click(object sender, EventArgs e)
        {
            try
            {
                switch (algorithm)
                {
                case Algorithm.Base64Unicode:
                    textBoxDecode.Text = TranString.DecodeBase64Unicode(textBoxEncode.Text);
                    break;

                case Algorithm.Base64:
                    textBoxDecode.Text = TranString.DecodeBase64(textBoxEncode.Text);
                    break;

                case Algorithm.UrlEncode:
                    textBoxDecode.Text = TranString.DecodeUrlDecode(textBoxEncode.Text);
                    break;

                case Algorithm.AES:
                    textBoxDecode.Text = TranString.DecodeAES(textBoxEncode.Text, textBoxKey.Text);
                    break;

                case Algorithm.Rijndael:
                    textBoxDecode.Text = TranString.DecodeRijndael(textBoxEncode.Text, textBoxRijndaelKey.Text, textBoxRijndaelVector.Text);
                    break;

                default:
                    throw new ArgumentOutOfRangeException($"algorithm : {algorithm}");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Exemple #2
0
        private void LoadDataList2Dictionary(List <string> listName, Dictionary <Tuple <Category, Key>, string> dicName)
        {
            lock (LockObj)
            {
                dicData.Clear();
                string data = string.Empty;
                foreach (string line in listName)
                {
                    try
                    {
                        if (!line.StartsWith(@"#") && !(line == null) && !(line.Trim().Equals("")))
                        {
                            string[] lineValues = line.Split(new string[] { ":::" }, StringSplitOptions.None);

                            if (lineValues[0].ToString().Equals("Base64Unicode", StringComparison.OrdinalIgnoreCase))
                            {
                                data = TranString.DecodeBase64Unicode(lineValues[3]);
                            }
                            else if (lineValues[0].ToString().Equals("Base64Ascii", StringComparison.OrdinalIgnoreCase))
                            {
                                data = TranString.DecodeBase64(lineValues[3]);
                            }
                            else
                            {
                                data = lineValues[3];
                            }

                            dicName.Add
                            (
                                new Tuple <Category, Key>
                                (
                                    (Category)Enum.Parse(typeof(Category), lineValues[1]),
                                    (Key)Enum.Parse(typeof(Key), lineValues[2])
                                ), data
                            );
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }

                //var logintype = dicData[new Tuple<Category, Key>(Category.Login, Key.LoginType)];
                //LoginType = (LoginType)Enum.Parse(typeof(LoginType), logintype);
            }
        }
Exemple #3
0
        public string GetCryptionKey(string getCryptionKey, string getCryptionKeyUrl, string accessKey, string SecretKey)
        {
            if (getCryptionKey.Equals("Local", StringComparison.OrdinalIgnoreCase))
            {
                return(GetValue(Category.Encryption, Key.LocalCryptionKey));
            }
            else if (getCryptionKey.Equals("NcpKms", StringComparison.OrdinalIgnoreCase))
            {
                for (int i = 0; i <= 3; i++)
                {
                    try
                    {
                        string _keytag     = GetValue(Category.Encryption, Key.KeyTag).Trim();
                        string _ciphertext = GetValue(Category.Encryption, Key.Ciphertext).Trim();

                        if (_keytag.Length == 0)
                        {
                            throw new Exception("keytag is empty. Please enter keytag");
                        }
                        if (_ciphertext.Length == 0)
                        {
                            throw new Exception("ciphertext is empty. Please enter ciphertext");
                        }

                        var kmsDecrypteParameters = new
                        {
                            ciphertext = _ciphertext
                        };
                        var    jt         = JToken.Parse(JsonConvert.SerializeObject(kmsDecrypteParameters));
                        string parameters = jt.ToString(Newtonsoft.Json.Formatting.Indented);

                        SoaCall asyncCall = new SoaCall();
                        var     response  = AsyncHelpers.RunSync <string>(() => asyncCall.WebApiCall(
                                                                              @"https://kms.apigw.ntruss.com",
                                                                              RequestType.POST,
                                                                              @"/keys/v2/" + _keytag + @"/decrypt",
                                                                              parameters,
                                                                              GetValue(Category.Api, Key.AccessKey),
                                                                              GetValue(Category.Api, Key.SecretKey), 5));

                        JsonSerializerSettings options = new JsonSerializerSettings
                        {
                            NullValueHandling     = NullValueHandling.Ignore,
                            MissingMemberHandling = MissingMemberHandling.Ignore
                        };

                        if (!response.Contains("SUCCESS"))
                        {
                            throw new Exception(response);
                        }

                        KmsDecryptResponse KmsDecryptResponse = JsonConvert.DeserializeObject <KmsDecryptResponse>(response, options);
                        return(TranString.DecodeBase64(KmsDecryptResponse.data.plaintext));
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine($"ncpkms not ready!, Message : {ex.Message}");
                    }
                    Thread.Sleep(2000);
                }
            }
            else // (getCryptionKey.Equals("RemoteKeyServer", StringComparison.OrdinalIgnoreCase))
            {
                for (int i = 0; i <= 3; i++)
                {
                    try
                    {
                        SoaCall asyncCall = new SoaCall();

                        var key = AsyncHelpers.RunSync <string>(() => asyncCall.WebApiCall(
                                                                    GetValue(Category.Encryption, Key.GetCryptionKeyUrl),
                                                                    RequestType.GET,
                                                                    @"/Api/GetCryptionKey",
                                                                    GetValue(Category.Api, Key.AccessKey),
                                                                    GetValue(Category.Api, Key.SecretKey), 5));

                        key = TranString.DecodeBase64((key.Replace("\"", "")));

                        return(key);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine($"KeyServer not ready!, Message : {ex.Message}");
                    }
                    Thread.Sleep(2000);
                }
            }
            throw new Exception("GET KEY ERROR");
        }
Exemple #4
0
        private void CheckConfigurationKey()
        {
            AppendVerifyLog("*. Encrytpion Key");

            string getCryptionKeyType = logClientConfig.GetValue(LogClient.Category.Encryption, LogClient.Key.GetCryptionKey);
            string keyTag             = LogClient.Config.Instance.GetValue(LogClient.Category.Encryption, LogClient.Key.KeyTag);
            string ciphertext         = LogClient.Config.Instance.GetValue(LogClient.Category.Encryption, LogClient.Key.Ciphertext);

            try
            {
                AppendVerifyLog($"   Current Encrytpion Type : {getCryptionKeyType}");
                if (getCryptionKeyType.Equals("Local", StringComparison.OrdinalIgnoreCase))
                {
                    AppendVerifyLog("   Cryption Key : " + logClientConfig.GetValue(Category.Encryption, Key.LocalCryptionKey));
                    if (logClientConfig.GetValue(Category.Encryption, Key.LocalCryptionKey).Length == 0)
                    {
                        AppendVerifyLog($"   [Warning] Cryption Key is too short! (key length is : {logClientConfig.GetValue(Category.Encryption, Key.LocalCryptionKey).Length})");
                    }
                }
                else
                {
                    if (keyTag.Length <= 1)
                    {
                        throw new Exception("   [ERROR] The KMS keytag is not corrent!");
                    }

                    if (ciphertext.Length <= 1)
                    {
                        throw new Exception("   [ERROR] The KMS ciphertext is not corrent!");
                    }

                    var kmsDecrypteParameters = new
                    {
                        ciphertext = ciphertext
                    };
                    var    jt         = JToken.Parse(JsonConvert.SerializeObject(kmsDecrypteParameters));
                    string parameters = jt.ToString(Newtonsoft.Json.Formatting.Indented);

                    SoaCall asyncCall = new SoaCall();
                    var     response  = AsyncHelpers.RunSync <string>(() => asyncCall.WebApiCall(
                                                                          @"https://kms.apigw.ntruss.com",
                                                                          RequestType.POST,
                                                                          @"/keys/v2/" + keyTag + @"/decrypt",
                                                                          parameters,
                                                                          LogClient.Config.Instance.GetValue(LogClient.Category.Api, LogClient.Key.AccessKey),
                                                                          LogClient.Config.Instance.GetValue(LogClient.Category.Api, LogClient.Key.SecretKey), 5));

                    JsonSerializerSettings options = new JsonSerializerSettings
                    {
                        NullValueHandling     = NullValueHandling.Ignore,
                        MissingMemberHandling = MissingMemberHandling.Ignore
                    };

                    if (!response.Contains("SUCCESS"))
                    {
                        throw new Exception(response);
                    }

                    KmsDecryptResponse KmsDecryptResponse = JsonConvert.DeserializeObject <KmsDecryptResponse>(response, options);
                    AppendVerifyLog("   Cryption KMS key : " + TranString.DecodeBase64(KmsDecryptResponse.data.plaintext));
                }

                AppendVerifyLog($"   Encryption Key Check Result : Success");
            }
            catch (Exception ex)
            {
                AppendVerifyLog(ex.Message);
                AppendVerifyLog("   Encryption Key(KMS) Help Message...");
                AppendVerifyLog("   -----------------------------------------------");
                AppendVerifyLog("   1. Enable subaccount in MC Console.");
                AppendVerifyLog("   2. In the Management Console, create a key for encryption / decryption.");
                AppendVerifyLog("   3. Paste the generated keytag into the SQL Server DBA Tool.");
                AppendVerifyLog("   4. In the SQL Server DBA Tool, type key");
                AppendVerifyLog("   5. Create ciphertext in the SQL Server DBA Tool.");
                AppendVerifyLog("   6. Save.");
                AppendVerifyLog("   -----------------------------------------------");
                throw new Exception("Encryption Key Error!");
            }
        }
Exemple #5
0
        private void buttonKeyTest_Click(object sender, EventArgs e)
        {
            if (radioButtonLocalKey.Checked)
            {
                MessageBox.Show(logClientConfig.GetValue(Category.Encryption, Key.LocalCryptionKey));
            }
            else //(radioButtonNcpKms.Checked)
            {
                try
                {
                    if (textBoxKeyTag.Text.Trim().Length == 0)
                    {
                        throw new Exception("keytag is empty. Please enter keytag");
                    }
                    if (textBoxCiphertext.Text.Trim().Length == 0)
                    {
                        throw new Exception("ciphertext is empty. Please enter ciphertext");
                    }

                    var kmsDecrypteParameters = new
                    {
                        ciphertext = textBoxCiphertext.Text
                    };
                    var    jt         = JToken.Parse(JsonConvert.SerializeObject(kmsDecrypteParameters));
                    string parameters = jt.ToString(Newtonsoft.Json.Formatting.Indented);

                    SoaCall asyncCall = new SoaCall();
                    var     response  = AsyncHelpers.RunSync <string>(() => asyncCall.WebApiCall(
                                                                          @"https://kms.apigw.ntruss.com",
                                                                          RequestType.POST,
                                                                          @"/keys/v2/" + textBoxKeyTag.Text + @"/decrypt",
                                                                          parameters,
                                                                          textBoxAccessKey.Text.Trim(),
                                                                          textBoxSecretKey.Text.Trim(), 5));

                    JsonSerializerSettings options = new JsonSerializerSettings
                    {
                        NullValueHandling     = NullValueHandling.Ignore,
                        MissingMemberHandling = MissingMemberHandling.Ignore
                    };

                    if (!response.Contains("SUCCESS"))
                    {
                        throw new Exception(response);
                    }

                    KmsDecryptResponse KmsDecryptResponse = JsonConvert.DeserializeObject <KmsDecryptResponse>(response, options);
                    MessageBox.Show(TranString.DecodeBase64(KmsDecryptResponse.data.plaintext));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            //else
            //{
            //    try
            //    {
            //        SoaCall asyncCall = new SoaCall();

            //        var key = AsyncHelpers.RunSync<string>(() => asyncCall.WebApiCall(
            //            textBoxRemoteKeyServerUrl.Text.Trim(),
            //            RequestType.GET,
            //            textBoxAction.Text.Trim(),
            //            textBoxAccessKey.Text.Trim(),
            //            textBoxSecretKey.Text.Trim(), 5));

            //        if (key.Contains("Endpoint not found."))
            //        {
            //            throw new Exception("Endpoint not found.");
            //        }
            //        key = TranString.DecodeBase64((key.Replace("\"", "")));
            //        if (key.Equals(""))
            //            MessageBox.Show("authentication error, check accessKey and secretKey");
            //        else
            //            MessageBox.Show(key);
            //    }
            //    catch (Exception ex)
            //    {
            //        if (ex.Message.Contains("A task was canceled."))
            //        {
            //            MessageBox.Show("Unable to connect to the remote server");
            //            return;
            //        }
            //        if (ex.InnerException != null)
            //        {
            //            MessageBox.Show(ex.InnerException.Message);
            //        }
            //        else
            //        {
            //            MessageBox.Show(ex.Message);
            //        }
            //    }
            //}
        }