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

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

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

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

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

                default:
                    throw new ArgumentOutOfRangeException($"algorithm : {algorithm}");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Example #2
0
        public void SaveUserData()
        {
            string        fullFilename = encryptedDataManagerContentsFullFilename;
            List <string> listName     = listData;

            lock (LockObj)
            {
                if (File.Exists(fullFilename))
                {
                    File.Delete(fullFilename);
                }


                if (LoginType.GOV == LoginType)
                {
                    dicData[new Tuple <Category, Key>(Category.Login, Key.LoginType)] = "GOV";
                }
                else
                {
                    dicData[new Tuple <Category, Key>(Category.Login, Key.LoginType)] = "Default";
                }

                var contents = new StringBuilder();

                foreach (string line in listName)
                {
                    try
                    {
                        if (line.StartsWith(@"#") || (line == null) || (line.Trim().Equals("")))
                        {
                            contents.Append(line + Environment.NewLine);
                        }
                        else
                        {
                            string[] lineValues = line.Split(new string[] { ":::" }, StringSplitOptions.None);
                            string   data       = string.Empty;
                            data = dicData[new Tuple <Category, Key>((Category)Enum.Parse(typeof(Category), lineValues[1]), (Key)Enum.Parse(typeof(Key), lineValues[2]))];
                            if (lineValues[0].ToString().ToUpper().Equals("Base64Unicode".ToUpper()))
                            {
                                data = TranString.EncodeBase64Unicode(data);
                            }
                            else if (lineValues[0].ToString().ToUpper().Equals("Base64Ascii".ToUpper()))
                            {
                                data = TranString.EncodeBase64(data);
                            }
                            contents.Append(lineValues[0] + ":::" + lineValues[1] + ":::" + lineValues[2] + ":::" + data + Environment.NewLine);
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }


                File.WriteAllText(fullFilename, TranString.EncodeAES(contents.ToString(), key));
            }
        }
Example #3
0
        private void buttonGetCiphertext_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBoxKey.Text.Trim().Length == 0)
                {
                    throw new Exception("key is null. Please enter key");
                }
                if (textBoxKeyTag.Text.Trim().Length == 0)
                {
                    throw new Exception("keytag is empty. Please enter keytag");
                }

                var kmsEncrypteParameters = new
                {
                    plaintext = TranString.EncodeBase64(textBoxKey.Text)
                };
                var    jt         = JToken.Parse(JsonConvert.SerializeObject(kmsEncrypteParameters));
                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 + @"/encrypt",
                                                                      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);
                }

                KmsEncryptResponse KmsEncryptResponse = JsonConvert.DeserializeObject <KmsEncryptResponse>(response, options);
                textBoxCiphertext.Text = KmsEncryptResponse.data.ciphertext;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Example #4
0
        private void WriteUpdateValue2File(List <string> listName, Dictionary <Tuple <Category, Key>, string> dicName, string filename)
        {
            lock (LockObj)
            {
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }

                using (StreamWriter file = new StreamWriter(filename))
                {
                    foreach (string line in listName)
                    {
                        try
                        {
                            if (line.StartsWith(@"#") || (line == null) || (line.Trim().Equals("")))
                            {
                                file.WriteLine(line);
                            }
                            else
                            {
                                string[] lineValues = line.Split(new string[] { ":::" }, StringSplitOptions.None);
                                string   data       = string.Empty;
                                data = dicData[new Tuple <Category, Key>((Category)Enum.Parse(typeof(Category), lineValues[1]), (Key)Enum.Parse(typeof(Key), lineValues[2]))];

                                if (lineValues[0].ToString().ToUpper().Equals("Base64Unicode".ToUpper()))
                                {
                                    data = TranString.EncodeBase64Unicode(data);
                                }
                                else if (lineValues[0].ToString().ToUpper().Equals("Base64Ascii".ToUpper()))
                                {
                                    data = TranString.EncodeBase64(data);
                                }

                                file.WriteLine(lineValues[0] + ":::" + lineValues[1] + ":::" + lineValues[2] + ":::" + data);
                            }
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                    }
                }
            }
        }
Example #5
0
        private string GetCreateServerInstancesJsonCommand()
        {
            try
            {
                List <KeyValuePair <string, string> > listKeyValueParameters = GetParameters();
                listKeyValueParameters.Add(new KeyValuePair <string, string>("responseFormatType", "json"));
                listKeyValueParameters.Add(new KeyValuePair <string, string>("userData", TranString.EncodeBase64(dataManager.GetValue(DataManager.Category.InitScript, DataManager.Key.userDataFinal))));
                Dictionary <string, string> dicParameters = new Dictionary <string, string>();

                foreach (var a in listKeyValueParameters)
                {
                    if (a.Value == null || a.Value.Equals("NULL", StringComparison.OrdinalIgnoreCase))
                    {
                    }
                    else
                    {
                        dicParameters.Add(a.Key, a.Value);
                    }
                }
                JToken jt = JToken.Parse(JsonConvert.SerializeObject(dicParameters));
                return(jt.ToString(Newtonsoft.Json.Formatting.Indented).Replace("_", "."));
            }
            catch (Exception)
            {
                throw;
            }
        }