Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using MySql.Data.MySqlClient;
- using System.Security.Cryptography;
- using System.IO;
- namespace Könsord {
- public partial class frmMain : Form {
- MySqlConnection Connection { get; set; }
- private string lastId;
- private string currentUser;
- private int color;
- Panel currentPanel;
- public frmMain() {
- InitializeComponent();
- #region Hide
- Connection = new MySqlConnection(
- "SERVER=LOCALHOST;DATABASE=seecret;UID=root;PASSWORD=secure");
- #endregion
- currentPanel = panel2;
- this.AcceptButton = btnConnect;
- }
- private void btnSend_Click(object sender, EventArgs e) {
- Connection.Open();
- if (!string.IsNullOrWhiteSpace(txtSend.Text)) {
- MySqlCommand c = new MySqlCommand(
- "INSERT INTO anteckningar (TEXT, DATE, USER)" +
- $"VALUES ('{txtSend.Text}', CURRENT_TIME(), '{currentUser}')", Connection);
- MySqlDataReader r = c.ExecuteReader();
- txtSend.Text = "";
- }
- Connection.Close();
- }
- private void timer1_Tick(object sender, EventArgs e) {
- Connection.Open();
- MySqlCommand c = new MySqlCommand(
- "SELECT * FROM anteckningar ORDER BY ID DESC LIMIT 1", Connection);
- MySqlDataReader r = c.ExecuteReader();
- string user = "";
- string id = "";
- string date = "";
- string text = "";
- while (r.Read()) {
- id = r["ID"].ToString();
- date = r["date"].ToString();
- text = r["text"].ToString();
- user = r["user"].ToString();
- }
- r.Close();
- c = new MySqlCommand(
- $"SELECT color FROM usernames where Username='{user}'", Connection);
- r = c.ExecuteReader();
- if (r.HasRows) {
- while (r.Read()) {
- if (lastId != id) {
- lastId = id;
- txtNote.AppendText(((string.IsNullOrWhiteSpace(txtNote.Text) ? "" : Environment.NewLine)) + date);
- txtNote.AppendText(" " + user, Color.FromArgb(int.Parse(r["color"].ToString())));
- txtNote.AppendText(" " + text);
- }
- }
- }
- else {
- if (lastId != id) {
- lastId = id;
- txtNote.AppendText((string.IsNullOrWhiteSpace(txtNote.Text) ? "" : Environment.NewLine) + date);
- txtNote.AppendText(" " + user, Color.Gray);
- txtNote.AppendText(" " + text);
- }
- }
- Connection.Close();
- }
- private void txtSend_Click(object sender, EventArgs e) {
- if (txtSend.Text == "Write here fgt...") {
- txtSend.Text = "";
- }
- }
- private void txtPwd_Click(object sender, EventArgs e) {
- if (txtPwd.Text == "Password...") {
- txtPwd.Text = "";
- }
- }
- private void txtUser_Click(object sender, EventArgs e) {
- if (txtUser.Text == "Username...") {
- txtUser.Text = "";
- }
- }
- private void btnConnect_Click(object sender, EventArgs e) {
- if (txtUser.Text.ToLower() == "guest") {
- Connection.Open();
- Random rand = new Random();
- currentUser = "Guest_" + rand.Next(int.MaxValue);
- color = Color.Gray.ToArgb();
- this.AcceptButton = btnSend;
- currentPanel.Visible = false;
- currentPanel = panel1;
- currentPanel.Visible = true;
- MySqlCommand c = new MySqlCommand(
- "SELECT * FROM anteckningar ORDER BY ID DESC LIMIT 10", Connection);
- MySqlDataReader r = c.ExecuteReader();
- List<string> eUser = new List<string>();
- List<string> eId = new List<string>();
- List<string> eDate = new List<string>();
- List<string> eText = new List<string>();
- while (r.Read()) {
- eId.Add(r["ID"].ToString());
- eDate.Add(r["date"].ToString());
- eText.Add(r["text"].ToString());
- eUser.Add(r["user"].ToString());
- }
- eId.Reverse();
- eUser.Reverse();
- eDate.Reverse();
- eText.Reverse();
- txtNote.Clear();
- for (int i = 0; i < eId.Count; i++) {
- r.Close();
- c = new MySqlCommand(
- $"SELECT color FROM usernames where Username='{eUser[i]}'", Connection);
- r = c.ExecuteReader();
- if (r.HasRows) {
- while (r.Read()) {
- if (lastId != eId[i]) {
- lastId = eId[i];
- txtNote.AppendText((string.IsNullOrWhiteSpace(txtNote.Text) ? "" : Environment.NewLine) + eDate[i]);
- txtNote.AppendText(" " + eUser[i], Color.FromArgb(int.Parse(r["color"].ToString())));
- txtNote.AppendText(" " + eText[i]);
- }
- }
- }
- else {
- if (lastId != eId[i]) {
- lastId = eId[i];
- txtNote.AppendText((string.IsNullOrWhiteSpace(txtNote.Text) ? "" : Environment.NewLine) + eDate[i]);
- txtNote.AppendText(" " + eUser[i], Color.Gray);
- txtNote.AppendText(" " + eText[i]);
- }
- }
- }
- Connection.Close();
- }
- else {
- Connection.Open();
- MySqlCommand c = new MySqlCommand(
- $"SELECT id from Usernames where Username='{txtUser.Text}'", Connection);
- MySqlDataReader r = c.ExecuteReader();
- if (r.HasRows) {
- string id = "";
- while (r.Read()) {
- id = r["id"].ToString();
- }
- r.Close();
- c = new MySqlCommand(
- $"SELECT * from Usernames where ID={id}", Connection);
- r = c.ExecuteReader();
- while (r.Read()) {
- if (r["Username"].ToString() == txtUser.Text && StringCipher.Decrypt(r["password"].ToString(), "onemoretime") == txtPwd.Text) {
- currentUser = txtUser.Text;
- color = int.Parse(r["color"].ToString());
- this.AcceptButton = btnSend;
- currentPanel.Visible = false;
- currentPanel = panel1;
- currentPanel.Visible = true;
- r.Close();
- c = new MySqlCommand(
- "SELECT * FROM anteckningar ORDER BY id desc LIMIT 10", Connection);
- r = c.ExecuteReader();
- List<string> eUser = new List<string>();
- List<string> eId = new List<string>();
- List<string> eDate = new List<string>();
- List<string> eText = new List<string>();
- while (r.Read()) {
- eId.Add(r["ID"].ToString());
- eDate.Add(r["date"].ToString());
- eText.Add(r["text"].ToString());
- eUser.Add(r["user"].ToString());
- }
- eId.Reverse();
- eUser.Reverse();
- eDate.Reverse();
- eText.Reverse();
- txtNote.Clear();
- for (int i = 0; i < eId.Count; i++) {
- r.Close();
- c = new MySqlCommand(
- $"SELECT color FROM usernames where Username='{eUser[i]}'", Connection);
- r = c.ExecuteReader();
- if (r.HasRows) {
- while (r.Read()) {
- lastId = eId[i];
- txtNote.AppendText((string.IsNullOrWhiteSpace(txtNote.Text) ? "" : Environment.NewLine) + eDate[i]);
- txtNote.AppendText(" " + eUser[i], Color.FromArgb(int.Parse(r["color"].ToString())));
- txtNote.AppendText(" " + eText[i]);
- }
- }
- else {
- lastId = eId[i];
- txtNote.AppendText((string.IsNullOrWhiteSpace(txtNote.Text) ? "" : Environment.NewLine) + eDate[i]);
- txtNote.AppendText(" " + eUser[i], Color.Gray);
- txtNote.AppendText(" " + eText[i]);
- }
- }
- }
- }
- }
- Connection.Close();
- }
- }
- private new void KeyDown(object sender, KeyEventArgs e) {
- if (e.KeyData == Keys.Enter) {
- btnConnect_Click(new object(), new EventArgs());
- }
- }
- private void KeyDownSend(object sender, KeyEventArgs e) {
- if (e.KeyData == Keys.Enter) {
- btnSend_Click(new object(), new EventArgs());
- }
- }
- private void txtNote_TextChanged(object sender, EventArgs e) {
- txtNote.SelectionStart = txtNote.Text.Length;
- txtNote.ScrollToCaret();
- }
- private void button1_Click_1(object sender, EventArgs e) {
- Form2 form2 = new Form2(this, Connection);
- form2.Visible = true;
- }
- private void button2_Click(object sender, EventArgs e) {
- Form3 form3 = new Form3(Connection, currentUser, color);
- form3.Visible = true;
- }
- private void button3_Click(object sender, EventArgs e) {
- currentUser = null;
- color = Color.Gray.ToArgb();
- currentPanel.Visible = false;
- txtNote.Clear();
- txtSend.Clear();
- txtUser.Text = "Username...";
- txtPwd.Text = "Password...";
- this.AcceptButton = btnConnect;
- currentPanel = panel2;
- currentPanel.Visible = true;
- }
- }
- public static class RichTextBoxExtensions {
- public static void AppendText(this RichTextBox box, string text, Color color) {
- box.SelectionStart = box.TextLength;
- box.SelectionLength = 0;
- box.SelectionColor = color;
- box.AppendText(text);
- box.SelectionColor = box.ForeColor;
- }
- }
- public static class StringCipher {
- // This constant is used to determine the keysize of the encryption algorithm in bits.
- // We divide this by 8 within the code below to get the equivalent number of bytes.
- private const int Keysize = 256;
- // This constant determines the number of iterations for the password bytes generation function.
- private const int DerivationIterations = 1000;
- public static string Encrypt(string plainText, string passPhrase) {
- // Salt and IV is randomly generated each time, but is preprended to encrypted cipher text
- // so that the same Salt and IV values can be used when decrypting.
- var saltStringBytes = Generate256BitsOfRandomEntropy();
- var ivStringBytes = Generate256BitsOfRandomEntropy();
- var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
- using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations)) {
- var keyBytes = password.GetBytes(Keysize / 8);
- using (var symmetricKey = new RijndaelManaged()) {
- symmetricKey.BlockSize = 256;
- symmetricKey.Mode = CipherMode.CBC;
- symmetricKey.Padding = PaddingMode.PKCS7;
- using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes)) {
- using (var memoryStream = new MemoryStream()) {
- using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) {
- cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
- cryptoStream.FlushFinalBlock();
- // Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes.
- var cipherTextBytes = saltStringBytes;
- cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray();
- cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
- memoryStream.Close();
- cryptoStream.Close();
- return Convert.ToBase64String(cipherTextBytes);
- }
- }
- }
- }
- }
- }
- public static string Decrypt(string cipherText, string passPhrase) {
- // Get the complete stream of bytes that represent:
- // [32 bytes of Salt] + [32 bytes of IV] + [n bytes of CipherText]
- var cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText);
- // Get the saltbytes by extracting the first 32 bytes from the supplied cipherText bytes.
- var saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray();
- // Get the IV bytes by extracting the next 32 bytes from the supplied cipherText bytes.
- var ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray();
- // Get the actual cipher text bytes by removing the first 64 bytes from the cipherText string.
- var cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray();
- using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations)) {
- var keyBytes = password.GetBytes(Keysize / 8);
- using (var symmetricKey = new RijndaelManaged()) {
- symmetricKey.BlockSize = 256;
- symmetricKey.Mode = CipherMode.CBC;
- symmetricKey.Padding = PaddingMode.PKCS7;
- using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes)) {
- using (var memoryStream = new MemoryStream(cipherTextBytes)) {
- using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) {
- var plainTextBytes = new byte[cipherTextBytes.Length];
- var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
- memoryStream.Close();
- cryptoStream.Close();
- return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
- }
- }
- }
- }
- }
- }
- private static byte[] Generate256BitsOfRandomEntropy() {
- var randomBytes = new byte[32]; // 32 Bytes will give us 256 bits.
- using (var rngCsp = new RNGCryptoServiceProvider()) {
- // Fill the array with cryptographically secure random bytes.
- rngCsp.GetBytes(randomBytes);
- }
- return randomBytes;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement