/// <summary>
        /// initialize page by AES key object
        /// </summary>
        /// <param name="aeskey">AES key object</param>
        /// <param name="encrypted">encrypted text ready to decrypt</param>
        public KeySetting(AESKey aeskey)
        {
            InitializeComponent();
            Key = aeskey;
            // mask AES key
            string tKey = aeskey.Key.Substring(0, 26).PadRight(32, '*');

            txt_key.Text    = tKey;
            txt_remark.Text = aeskey.Remark;
            BackFromSetting = true;
        }
Exemple #2
0
        /// <summary>
        /// check clipboard content
        /// </summary>
        private async void CheckClipboard()
        {
            string text = await Clipboard.GetTextAsync();

            string head = "UnderText://";

            try
            {
                if (text.StartsWith(head))
                {
                    text = text.Substring(text.IndexOf(head) + head.Length);
                    // get aes key encrypted by rsa public key
                    // then search key list
                    if (text.StartsWith("."))
                    {
                        byte[] data = Convert.FromBase64String(text.Substring(1));
                        string dec  = Encoding.ASCII.GetString(rsaProvider.Decrypt(data, false));
                        if (AESKey.Validate(dec))
                        {
                            bool no_key = true;
                            foreach (var key in AESKeys)
                            {
                                if (key.Key.Equals(dec))
                                {
                                    no_key = false;
                                    break;
                                }
                            }
                            if (no_key)
                            {
                                AESKey newKey = new AESKey(dec);
                                AESKeys.Add(newKey);
                                await Navigation.PushAsync(new KeySetting(newKey));
                            }
                        }
                    }
                    // get rsa public key
                    // then generate aes key and send
                    else if (text.StartsWith(","))
                    {
                        string PK_recved = Encoding.ASCII.GetString(Convert.FromBase64String(text.Substring(1)));
                        if (!PK_recved.Equals(PK))
                        {
                            RSACryptoServiceProvider tRSAProvider = new RSACryptoServiceProvider(2048);
                            tRSAProvider.FromXmlString(PK_recved);
                            AESKey newKey = new AESKey(AESKey.Generate());
                            AESKeys.Add(newKey);
                            await Clipboard.SetTextAsync("UnderText://." + Convert.ToBase64String(tRSAProvider.Encrypt(Encoding.ASCII.GetBytes(newKey.Key), false)));
                            await DisplayAlert("Handshake(Step 2)", "AES packet has set to clipboard,\npaste and send the string to partner(s) who send you RSA packet,\nthen others will have the AES key.", "OK");

                            await Navigation.PushAsync(new KeySetting(newKey));
                        }
                    }
                    // get text data encrypted by aes key
                    // find right aes key in the list to decrypt
                    else
                    {
                        foreach (var key in AESKeys)
                        {
                            if (key.Decrypt(text) != string.Empty)
                            {
                                await Navigation.PushAsync(new KeySetting(key));

                                break;
                            }
                        }
                    }
                }
            }
            catch
            {
                ;
            }
        }