Advertisement
Guest User

Untitled

a guest
May 23rd, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using MySql.Data.MySqlClient;
  11. using System.Security.Cryptography;
  12. using System.IO;
  13.  
  14. namespace Könsord {
  15.     public partial class frmMain : Form {
  16.  
  17.         MySqlConnection Connection { get; set; }
  18.         private string lastId;
  19.         private string currentUser;
  20.         private int color;
  21.         Panel currentPanel;
  22.  
  23.         public frmMain() {
  24.             InitializeComponent();
  25.             #region Hide
  26.             Connection = new MySqlConnection(
  27.                 "SERVER=LOCALHOST;DATABASE=seecret;UID=root;PASSWORD=secure");
  28.             #endregion
  29.             currentPanel = panel2;
  30.             this.AcceptButton = btnConnect;
  31.         }
  32.  
  33.  
  34.         private void btnSend_Click(object sender, EventArgs e) {
  35.             Connection.Open();
  36.             if (!string.IsNullOrWhiteSpace(txtSend.Text)) {
  37.                 MySqlCommand c = new MySqlCommand(
  38.                 "INSERT INTO anteckningar (TEXT, DATE, USER)" +
  39.                 $"VALUES ('{txtSend.Text}', CURRENT_TIME(), '{currentUser}')", Connection);
  40.                 MySqlDataReader r = c.ExecuteReader();
  41.                 txtSend.Text = "";
  42.             }
  43.  
  44.  
  45.             Connection.Close();
  46.         }
  47.  
  48.         private void timer1_Tick(object sender, EventArgs e) {
  49.             Connection.Open();
  50.             MySqlCommand c = new MySqlCommand(
  51.                 "SELECT * FROM anteckningar ORDER BY ID DESC LIMIT 1", Connection);
  52.             MySqlDataReader r = c.ExecuteReader();
  53.             string user = "";
  54.             string id = "";
  55.             string date = "";
  56.             string text = "";
  57.  
  58.             while (r.Read()) {
  59.                 id = r["ID"].ToString();
  60.                 date = r["date"].ToString();
  61.                 text = r["text"].ToString();
  62.                 user = r["user"].ToString();
  63.             }
  64.  
  65.             r.Close();
  66.             c = new MySqlCommand(
  67.                 $"SELECT color FROM usernames where Username='{user}'", Connection);
  68.             r = c.ExecuteReader();
  69.             if (r.HasRows) {
  70.                 while (r.Read()) {
  71.                     if (lastId != id) {
  72.  
  73.                         lastId = id;
  74.                         txtNote.AppendText(((string.IsNullOrWhiteSpace(txtNote.Text) ? "" : Environment.NewLine)) + date);
  75.                         txtNote.AppendText(" " + user, Color.FromArgb(int.Parse(r["color"].ToString())));
  76.                         txtNote.AppendText(" " + text);
  77.                     }
  78.                 }
  79.             }
  80.             else {
  81.                 if (lastId != id) {
  82.                     lastId = id;
  83.                     txtNote.AppendText((string.IsNullOrWhiteSpace(txtNote.Text) ? "" : Environment.NewLine) + date);
  84.                     txtNote.AppendText(" " + user, Color.Gray);
  85.                     txtNote.AppendText(" " + text);
  86.                 }
  87.             }
  88.  
  89.  
  90.             Connection.Close();
  91.         }
  92.  
  93.         private void txtSend_Click(object sender, EventArgs e) {
  94.             if (txtSend.Text == "Write here fgt...") {
  95.                 txtSend.Text = "";
  96.             }
  97.         }
  98.  
  99.         private void txtPwd_Click(object sender, EventArgs e) {
  100.             if (txtPwd.Text == "Password...") {
  101.                 txtPwd.Text = "";
  102.             }
  103.         }
  104.  
  105.         private void txtUser_Click(object sender, EventArgs e) {
  106.             if (txtUser.Text == "Username...") {
  107.                 txtUser.Text = "";
  108.             }
  109.         }
  110.  
  111.         private void btnConnect_Click(object sender, EventArgs e) {
  112.             if (txtUser.Text.ToLower() == "guest") {
  113.                 Connection.Open();
  114.  
  115.                 Random rand = new Random();
  116.                 currentUser = "Guest_" + rand.Next(int.MaxValue);
  117.                 color = Color.Gray.ToArgb();
  118.  
  119.                 this.AcceptButton = btnSend;
  120.                 currentPanel.Visible = false;
  121.                 currentPanel = panel1;
  122.                 currentPanel.Visible = true;
  123.  
  124.                 MySqlCommand c = new MySqlCommand(
  125.     "SELECT * FROM anteckningar ORDER BY ID DESC LIMIT 10", Connection);
  126.                 MySqlDataReader r = c.ExecuteReader();
  127.                 List<string> eUser = new List<string>();
  128.                 List<string> eId = new List<string>();
  129.                 List<string> eDate = new List<string>();
  130.                 List<string> eText = new List<string>();
  131.                
  132.  
  133.                 while (r.Read()) {
  134.                     eId.Add(r["ID"].ToString());
  135.                     eDate.Add(r["date"].ToString());
  136.                     eText.Add(r["text"].ToString());
  137.                     eUser.Add(r["user"].ToString());
  138.                 }
  139.  
  140.                 eId.Reverse();
  141.                 eUser.Reverse();
  142.                 eDate.Reverse();
  143.                 eText.Reverse();
  144.                 txtNote.Clear();
  145.  
  146.                 for (int i = 0; i < eId.Count; i++) {
  147.                     r.Close();
  148.  
  149.                     c = new MySqlCommand(
  150.                     $"SELECT color FROM usernames where Username='{eUser[i]}'", Connection);
  151.                     r = c.ExecuteReader();
  152.                     if (r.HasRows) {
  153.                         while (r.Read()) {
  154.                             if (lastId != eId[i]) {
  155.  
  156.                                 lastId = eId[i];
  157.                                 txtNote.AppendText((string.IsNullOrWhiteSpace(txtNote.Text) ? "" : Environment.NewLine) + eDate[i]);
  158.                                 txtNote.AppendText(" " + eUser[i], Color.FromArgb(int.Parse(r["color"].ToString())));
  159.                                 txtNote.AppendText(" " + eText[i]);
  160.                             }
  161.                         }
  162.                     }
  163.                     else {
  164.                         if (lastId != eId[i]) {
  165.                             lastId = eId[i];
  166.                             txtNote.AppendText((string.IsNullOrWhiteSpace(txtNote.Text) ? "" : Environment.NewLine) + eDate[i]);
  167.                             txtNote.AppendText(" " + eUser[i], Color.Gray);
  168.                             txtNote.AppendText(" " + eText[i]);
  169.                         }
  170.                     }
  171.                 }
  172.                 Connection.Close();
  173.             }
  174.             else {
  175.                 Connection.Open();
  176.  
  177.                 MySqlCommand c = new MySqlCommand(
  178.                 $"SELECT id from Usernames where Username='{txtUser.Text}'", Connection);
  179.                 MySqlDataReader r = c.ExecuteReader();
  180.  
  181.                 if (r.HasRows) {
  182.                     string id = "";
  183.                     while (r.Read()) {
  184.                         id = r["id"].ToString();
  185.                     }
  186.                     r.Close();
  187.                     c = new MySqlCommand(
  188.                         $"SELECT * from Usernames where ID={id}", Connection);
  189.                     r = c.ExecuteReader();
  190.                     while (r.Read()) {
  191.                         if (r["Username"].ToString() == txtUser.Text && StringCipher.Decrypt(r["password"].ToString(), "onemoretime") == txtPwd.Text) {
  192.                             currentUser = txtUser.Text;
  193.                             color = int.Parse(r["color"].ToString());
  194.  
  195.                             this.AcceptButton = btnSend;
  196.                             currentPanel.Visible = false;
  197.                             currentPanel = panel1;
  198.                             currentPanel.Visible = true;
  199.  
  200.                             r.Close();
  201.  
  202.                             c = new MySqlCommand(
  203.                 "SELECT * FROM anteckningar ORDER BY id desc LIMIT 10", Connection);
  204.                             r = c.ExecuteReader();
  205.                             List<string> eUser = new List<string>();
  206.                             List<string> eId = new List<string>();
  207.                             List<string> eDate = new List<string>();
  208.                             List<string> eText = new List<string>();
  209.                             while (r.Read()) {
  210.                                 eId.Add(r["ID"].ToString());
  211.                                 eDate.Add(r["date"].ToString());
  212.                                 eText.Add(r["text"].ToString());
  213.                                 eUser.Add(r["user"].ToString());
  214.                             }
  215.  
  216.                             eId.Reverse();
  217.                             eUser.Reverse();
  218.                             eDate.Reverse();
  219.                             eText.Reverse();
  220.                             txtNote.Clear();
  221.  
  222.  
  223.                             for (int i = 0; i < eId.Count; i++) {
  224.                                 r.Close();
  225.  
  226.                                 c = new MySqlCommand(
  227.                                 $"SELECT color FROM usernames where Username='{eUser[i]}'", Connection);
  228.                                 r = c.ExecuteReader();
  229.                                 if (r.HasRows) {
  230.                                     while (r.Read()) {
  231.                                         lastId = eId[i];
  232.                                         txtNote.AppendText((string.IsNullOrWhiteSpace(txtNote.Text) ? "" : Environment.NewLine) + eDate[i]);
  233.                                         txtNote.AppendText(" " + eUser[i], Color.FromArgb(int.Parse(r["color"].ToString())));
  234.                                         txtNote.AppendText(" " + eText[i]);
  235.  
  236.                                     }
  237.                                 }
  238.                                 else {
  239.  
  240.                                     lastId = eId[i];
  241.                                     txtNote.AppendText((string.IsNullOrWhiteSpace(txtNote.Text) ? "" : Environment.NewLine) + eDate[i]);
  242.                                     txtNote.AppendText(" " + eUser[i], Color.Gray);
  243.                                     txtNote.AppendText(" " + eText[i]);
  244.  
  245.                                 }
  246.                             }
  247.  
  248.                         }
  249.                     }
  250.                 }
  251.  
  252.                 Connection.Close();
  253.             }
  254.         }
  255.  
  256.         private new void KeyDown(object sender, KeyEventArgs e) {
  257.             if (e.KeyData == Keys.Enter) {
  258.                 btnConnect_Click(new object(), new EventArgs());
  259.             }
  260.         }
  261.  
  262.         private void KeyDownSend(object sender, KeyEventArgs e) {
  263.             if (e.KeyData == Keys.Enter) {
  264.                 btnSend_Click(new object(), new EventArgs());
  265.             }
  266.         }
  267.  
  268.         private void txtNote_TextChanged(object sender, EventArgs e) {
  269.             txtNote.SelectionStart = txtNote.Text.Length;
  270.             txtNote.ScrollToCaret();
  271.         }
  272.  
  273.         private void button1_Click_1(object sender, EventArgs e) {
  274.             Form2 form2 = new Form2(this, Connection);
  275.             form2.Visible = true;
  276.         }
  277.  
  278.         private void button2_Click(object sender, EventArgs e) {
  279.             Form3 form3 = new Form3(Connection, currentUser, color);
  280.             form3.Visible = true;
  281.         }
  282.  
  283.         private void button3_Click(object sender, EventArgs e) {
  284.             currentUser = null;
  285.             color = Color.Gray.ToArgb();
  286.             currentPanel.Visible = false;
  287.             txtNote.Clear();
  288.             txtSend.Clear();
  289.             txtUser.Text = "Username...";
  290.             txtPwd.Text = "Password...";
  291.             this.AcceptButton = btnConnect;
  292.             currentPanel = panel2;
  293.             currentPanel.Visible = true;
  294.         }
  295.     }
  296.  
  297.     public static class RichTextBoxExtensions {
  298.         public static void AppendText(this RichTextBox box, string text, Color color) {
  299.             box.SelectionStart = box.TextLength;
  300.             box.SelectionLength = 0;
  301.  
  302.             box.SelectionColor = color;
  303.             box.AppendText(text);
  304.             box.SelectionColor = box.ForeColor;
  305.         }
  306.     }
  307.  
  308.     public static class StringCipher {
  309.         // This constant is used to determine the keysize of the encryption algorithm in bits.
  310.         // We divide this by 8 within the code below to get the equivalent number of bytes.
  311.         private const int Keysize = 256;
  312.  
  313.         // This constant determines the number of iterations for the password bytes generation function.
  314.         private const int DerivationIterations = 1000;
  315.  
  316.         public static string Encrypt(string plainText, string passPhrase) {
  317.             // Salt and IV is randomly generated each time, but is preprended to encrypted cipher text
  318.             // so that the same Salt and IV values can be used when decrypting.  
  319.             var saltStringBytes = Generate256BitsOfRandomEntropy();
  320.             var ivStringBytes = Generate256BitsOfRandomEntropy();
  321.             var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
  322.             using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations)) {
  323.                 var keyBytes = password.GetBytes(Keysize / 8);
  324.                 using (var symmetricKey = new RijndaelManaged()) {
  325.                     symmetricKey.BlockSize = 256;
  326.                     symmetricKey.Mode = CipherMode.CBC;
  327.                     symmetricKey.Padding = PaddingMode.PKCS7;
  328.                     using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes)) {
  329.                         using (var memoryStream = new MemoryStream()) {
  330.                             using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) {
  331.                                 cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
  332.                                 cryptoStream.FlushFinalBlock();
  333.                                 // Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes.
  334.                                 var cipherTextBytes = saltStringBytes;
  335.                                 cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray();
  336.                                 cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
  337.                                 memoryStream.Close();
  338.                                 cryptoStream.Close();
  339.                                 return Convert.ToBase64String(cipherTextBytes);
  340.                             }
  341.                         }
  342.                     }
  343.                 }
  344.             }
  345.         }
  346.  
  347.         public static string Decrypt(string cipherText, string passPhrase) {
  348.             // Get the complete stream of bytes that represent:
  349.             // [32 bytes of Salt] + [32 bytes of IV] + [n bytes of CipherText]
  350.             var cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText);
  351.             // Get the saltbytes by extracting the first 32 bytes from the supplied cipherText bytes.
  352.             var saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray();
  353.             // Get the IV bytes by extracting the next 32 bytes from the supplied cipherText bytes.
  354.             var ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray();
  355.             // Get the actual cipher text bytes by removing the first 64 bytes from the cipherText string.
  356.             var cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray();
  357.  
  358.             using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations)) {
  359.                 var keyBytes = password.GetBytes(Keysize / 8);
  360.                 using (var symmetricKey = new RijndaelManaged()) {
  361.                     symmetricKey.BlockSize = 256;
  362.                     symmetricKey.Mode = CipherMode.CBC;
  363.                     symmetricKey.Padding = PaddingMode.PKCS7;
  364.                     using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes)) {
  365.                         using (var memoryStream = new MemoryStream(cipherTextBytes)) {
  366.                             using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) {
  367.                                 var plainTextBytes = new byte[cipherTextBytes.Length];
  368.                                 var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
  369.                                 memoryStream.Close();
  370.                                 cryptoStream.Close();
  371.                                 return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
  372.                             }
  373.                         }
  374.                     }
  375.                 }
  376.             }
  377.         }
  378.  
  379.         private static byte[] Generate256BitsOfRandomEntropy() {
  380.             var randomBytes = new byte[32]; // 32 Bytes will give us 256 bits.
  381.             using (var rngCsp = new RNGCryptoServiceProvider()) {
  382.                 // Fill the array with cryptographically secure random bytes.
  383.                 rngCsp.GetBytes(randomBytes);
  384.             }
  385.             return randomBytes;
  386.         }
  387.     }
  388. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement