Пример #1
0
        private void GetMail()
        {
            string mailfrom = logform.getUsrName();
            string mailpass = logform.getUsrPass();

            try {
                Task.Run(() =>
                {
                    using (ImapClient client = new ImapClient("imap.gmail.com", 993, mailfrom, mailpass, AuthMethod.Login, true))
                    {
                        if (client.Supports("IDLE") == false)
                        {
                            MessageBox.Show("Server do not support IMAP IDLE");
                            return;
                        }
                        string cryptedAppSign = AesCrypt.Encrypt("UrfaDiyarbakir");

                        var uids  = client.Search(SearchCondition.Text(cryptedAppSign));
                        int index = 0;


                        foreach (var id in uids)
                        {
                            var m = client.GetMessage(id, FetchOptions.Normal);


                            int a = m.Body.IndexOf("---AppSign---") + "---AppSign---".Length;
                            int b = m.Body.IndexOf("---Username---");

                            int c = m.Body.IndexOf("---Username---") + "---Username---".Length;
                            int d = m.Body.IndexOf("---Body---");

                            int e = m.Body.IndexOf("---Body---") + "---Body---".Length;
                            int f = m.Body.IndexOf("---Sign---");

                            string appsign       = m.Body.Substring(0, m.Body.IndexOf("---AppSign---"));
                            string Username      = AesCrypt.Decrypt(m.Body.Substring(a, b - a));
                            string sign          = m.Body.Substring(e, f - e);
                            string encryptedBody = m.Body.Substring(c, d - c);
                            string dir           = EncryptedMailApp.LoginForm.user;


                            char[] charsToTrim  = { ' ', '"' };
                            string encryptedAES = FbClient.Get(EncryptedMailApp.LoginForm.user + "/friends/" + Username + "/aes").Body.ToString().Trim(charsToTrim);
                            string friendPublic = FbClient.Get(EncryptedMailApp.LoginForm.user + "/friends/" + Username + "/publicKey").Body.ToString().Trim(charsToTrim);
                            string aes;
                            string decryptedBody;
                            string decryptedSubject;
                            try
                            {
                                string decryptetaes = RSAobj.Decrypt(encryptedAES);
                                aes = decryptetaes;
                            }
                            catch
                            {
                                aes = null;
                                Console.WriteLine("error on decrypting aes ");
                            }
                            try
                            {
                                decryptedBody = AesCrypt.DecryptParam(encryptedBody, aes);
                            }
                            catch
                            {
                                decryptedBody = null;
                                Console.WriteLine("error on decrpting body");
                            }
                            try
                            {
                                decryptedSubject = AesCrypt.DecryptParam(m.Subject, aes);
                            }
                            catch
                            {
                                decryptedSubject = null;
                                Console.WriteLine("error on decrpting subject");
                            }
                            if (RSAobj.VerifySign(decryptedBody, sign, friendPublic) == true)
                            {
                                Console.WriteLine("match");
                                signed = true;
                            }
                            else
                            {
                                Console.WriteLine("missmatch");
                                signed = false;
                            }

                            if (messages[index, 0] == null)
                            {
                                messages[index, 0] = Username;
                                messages[index, 1] = decryptedSubject;
                                messages[index, 2] = decryptedBody;
                            }

                            listBox1.Items.Add(new ListItem {
                                Name = "From:   " + Username + "   Subject:   " + decryptedSubject, Value = index.ToString()
                            });

                            index++;

                            foreach (Attachment attachment in m.Attachments)
                            {
                                byte[] allBytes = new byte[attachment.ContentStream.Length];
                                int bytesread   = attachment.ContentStream.Read(allBytes, 0, (int)attachment.ContentStream.Length);
                                if (System.IO.Directory.Exists("data\\" + dir + "\\attachments") == false)
                                {
                                    System.IO.Directory.CreateDirectory("data\\" + dir + "\\attachments");
                                }
                                string destinationFile = "data\\" + dir + "\\attachments\\" + attachment.Name;
                                BinaryWriter writer    = new BinaryWriter(new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
                                writer.Write(allBytes);
                                writer.Close();
                                if (attachment.Name.EndsWith("crp"))
                                {
                                    EncryptedMailApp.CryptoStuff.DecryptFile(password, destinationFile, destinationFile.Replace("crp", ""));
                                }
                                //MessageBox.Show("saved attachment at attachments, attachment count is : "+ m.Attachments.Count);
                            }
                        }

                        this.listBox1.MouseDoubleClick += new MouseEventHandler(listBox1_MouseDoubleClick);
                    }
                });
            }
            catch (InvalidCredentialsException)
            {
                MessageBox.Show("The server rejected the supplied credentials.");
            }
        }