Şifreli Mesajlaşma - Diffie Hellman + AES

Diffie Hellman, uçtan uca şifreleme için güvenli bir ortak anahtar oluşturma algoritmasıdır. AES ise bir şifreleme algoritmasıdır.

Diffie Hellman anahtar değişimi algoritması uçtan uca şifreleme için güvenli bir ortak anahtar oluşturma algoritmasıdır. AES ise bir şifreleme algoritmasıdır. AES için kullanılan anahtar ortak anahtardır. Ayrıca bir başlangıç vektörü (initialization vector - IV) de üretilir ve bu vektör iki uç tarafından da bilinmelidir.

FormMain.cs

using System;
using System.IO;
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 System.Security.Cryptography;

namespace Secure_Messaging_Sample
{
    public partial class FormMain : Form
    {
        byte[] person1PublicKey, person2PublicKey;
        byte[] commonKey;
        byte[] IV;
        byte[] encryptedMessage1, encryptedMessage2;

        public FormMain()
        {
            InitializeComponent();
        }

        private void GenerateKeys()
        {
            using (ECDiffieHellmanCng ecd1 = new ECDiffieHellmanCng())
            {
                ecd1.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
                ecd1.HashAlgorithm = CngAlgorithm.Sha256;
                person1PublicKey = ecd1.PublicKey.ToByteArray();
                textBoxP1Public.Text = Convert.ToBase64String(person1PublicKey);

                /* 2. kişi tarafındaki işlemler, 1. kişinin açık anahtarı biliniyor. */
                using (ECDiffieHellmanCng ecd2 = new ECDiffieHellmanCng())
                {
                    ecd2.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
                    ecd2.HashAlgorithm = CngAlgorithm.Sha256;

                    person2PublicKey = ecd2.PublicKey.ToByteArray();
                    textBoxP2Public.Text = Convert.ToBase64String(person2PublicKey);

                    commonKey = ecd2.DeriveKeyMaterial(CngKey.Import(person1PublicKey, CngKeyBlobFormat.EccPublicBlob));
                    textBoxCommonKey.Text = Convert.ToBase64String(commonKey);

                }
            }
        }

        private void personSendBtn_Click(object sender, EventArgs e)
        {
            bool person1To2 = int.Parse(((Button)sender).Tag.ToString()) == 1;

            string messageToSend = person1To2 ? person1SendText.Text : person2SendText.Text;

            if (person1PublicKey == null || person1PublicKey.Length <= 0)
                GenerateKeys();

            // Gönderiliyor

            using (Aes aes = new AesCryptoServiceProvider())
            {
                aes.Key = commonKey;

                if (IV == null || IV.Length <= 0)
                    IV = aes.IV;
                else
                    aes.IV = IV;

                textBoxIV.Text = Convert.ToBase64String(IV);

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        byte[] plainTextMessage = Encoding.UTF8.GetBytes(messageToSend);
                        cs.Write(plainTextMessage, 0, plainTextMessage.Length);
                        cs.Close();

                        if (person1To2)
                        {
                            encryptedMessage1 = ms.ToArray();
                            textBoxEncrypted1.Text = Convert.ToBase64String(encryptedMessage1);
                        }
                        else
                        {
                            encryptedMessage2 = ms.ToArray();
                            textBoxEncrypted2.Text = Convert.ToBase64String(encryptedMessage2);
                        }
                    }
                }
            }

            // Alınıyor

            using (Aes aes = new AesCryptoServiceProvider())
            {
                aes.Key = commonKey;
                aes.IV = IV;

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        if (person1To2)
                            cs.Write(encryptedMessage1, 0, encryptedMessage1.Length);
                        else
                            cs.Write(encryptedMessage2, 0, encryptedMessage2.Length);

                        cs.Close();

                        string receivedMessage = Encoding.UTF8.GetString(ms.ToArray());

                        if (person1To2)
                            person2ReceivedText.Text = receivedMessage;
                        else
                            person1ReceivedText.Text = receivedMessage;
                    }
                }
            }
        }
    }
}

FormMain.Designer.cs

namespace Secure_Messaging_Sample
{
    partial class FormMain
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.person1ReceivedText = new System.Windows.Forms.TextBox();
            this.person1SendBtn = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.label10 = new System.Windows.Forms.Label();
            this.person1SendText = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.textBoxEncrypted2 = new System.Windows.Forms.TextBox();
            this.textBoxP1Public = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.label9 = new System.Windows.Forms.Label();
            this.textBoxEncrypted1 = new System.Windows.Forms.TextBox();
            this.person2SendBtn = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.textBoxP2Public = new System.Windows.Forms.TextBox();
            this.label6 = new System.Windows.Forms.Label();
            this.person2SendText = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.person2ReceivedText = new System.Windows.Forms.TextBox();
            this.textBoxCommonKey = new System.Windows.Forms.TextBox();
            this.label8 = new System.Windows.Forms.Label();
            this.textBoxIV = new System.Windows.Forms.TextBox();
            this.label11 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.label12 = new System.Windows.Forms.Label();
            this.label13 = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.panel2 = new System.Windows.Forms.Panel();
            this.panel3 = new System.Windows.Forms.Panel();
            this.panel4 = new System.Windows.Forms.Panel();
            this.panel5 = new System.Windows.Forms.Panel();
            this.panel6 = new System.Windows.Forms.Panel();
            this.panel7 = new System.Windows.Forms.Panel();
            this.panel8 = new System.Windows.Forms.Panel();
            this.label14 = new System.Windows.Forms.Label();
            this.label15 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // person1ReceivedText
            // 
            this.person1ReceivedText.BackColor = System.Drawing.SystemColors.Control;
            this.person1ReceivedText.Location = new System.Drawing.Point(35, 467);
            this.person1ReceivedText.Multiline = true;
            this.person1ReceivedText.Name = "person1ReceivedText";
            this.person1ReceivedText.Size = new System.Drawing.Size(415, 55);
            this.person1ReceivedText.TabIndex = 0;
            // 
            // person1SendBtn
            // 
            this.person1SendBtn.Location = new System.Drawing.Point(334, 609);
            this.person1SendBtn.Name = "person1SendBtn";
            this.person1SendBtn.Size = new System.Drawing.Size(116, 29);
            this.person1SendBtn.TabIndex = 2;
            this.person1SendBtn.Tag = "1";
            this.person1SendBtn.Text = "GÖNDER";
            this.person1SendBtn.UseVisualStyleBackColor = true;
            this.person1SendBtn.Click += new System.EventHandler(this.personSendBtn_Click);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.ForeColor = System.Drawing.Color.Green;
            this.label2.Location = new System.Drawing.Point(35, 527);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(144, 18);
            this.label2.TabIndex = 3;
            this.label2.Text = "Gönderilecek mesaj:";
            // 
            // label10
            // 
            this.label10.AutoSize = true;
            this.label10.ForeColor = System.Drawing.Color.Crimson;
            this.label10.Location = new System.Drawing.Point(35, 350);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(145, 18);
            this.label10.TabIndex = 14;
            this.label10.Text = "Gelen Mesaj (Şifreli):";
            // 
            // person1SendText
            // 
            this.person1SendText.Location = new System.Drawing.Point(35, 548);
            this.person1SendText.Multiline = true;
            this.person1SendText.Name = "person1SendText";
            this.person1SendText.Size = new System.Drawing.Size(415, 55);
            this.person1SendText.TabIndex = 0;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.ForeColor = System.Drawing.Color.Crimson;
            this.label1.Location = new System.Drawing.Point(35, 446);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(95, 18);
            this.label1.TabIndex = 1;
            this.label1.Text = "Gelen Mesaj:";
            // 
            // textBoxEncrypted2
            // 
            this.textBoxEncrypted2.BackColor = System.Drawing.SystemColors.Control;
            this.textBoxEncrypted2.Location = new System.Drawing.Point(35, 371);
            this.textBoxEncrypted2.Multiline = true;
            this.textBoxEncrypted2.Name = "textBoxEncrypted2";
            this.textBoxEncrypted2.Size = new System.Drawing.Size(415, 65);
            this.textBoxEncrypted2.TabIndex = 13;
            // 
            // textBoxP1Public
            // 
            this.textBoxP1Public.Location = new System.Drawing.Point(29, 82);
            this.textBoxP1Public.Multiline = true;
            this.textBoxP1Public.Name = "textBoxP1Public";
            this.textBoxP1Public.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.textBoxP1Public.Size = new System.Drawing.Size(415, 126);
            this.textBoxP1Public.TabIndex = 4;
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(26, 61);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(177, 18);
            this.label5.TabIndex = 3;
            this.label5.Text = "Açık Anahtar (Public Key):";
            // 
            // label9
            // 
            this.label9.AutoSize = true;
            this.label9.ForeColor = System.Drawing.Color.Green;
            this.label9.Location = new System.Drawing.Point(555, 350);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(145, 18);
            this.label9.TabIndex = 12;
            this.label9.Text = "Gelen Mesaj (Şifreli):";
            // 
            // textBoxEncrypted1
            // 
            this.textBoxEncrypted1.BackColor = System.Drawing.SystemColors.Control;
            this.textBoxEncrypted1.Location = new System.Drawing.Point(561, 371);
            this.textBoxEncrypted1.Multiline = true;
            this.textBoxEncrypted1.Name = "textBoxEncrypted1";
            this.textBoxEncrypted1.Size = new System.Drawing.Size(415, 65);
            this.textBoxEncrypted1.TabIndex = 11;
            // 
            // person2SendBtn
            // 
            this.person2SendBtn.Location = new System.Drawing.Point(863, 609);
            this.person2SendBtn.Name = "person2SendBtn";
            this.person2SendBtn.Size = new System.Drawing.Size(116, 29);
            this.person2SendBtn.TabIndex = 2;
            this.person2SendBtn.Tag = "2";
            this.person2SendBtn.Text = "GÖNDER";
            this.person2SendBtn.UseVisualStyleBackColor = true;
            this.person2SendBtn.Click += new System.EventHandler(this.personSendBtn_Click);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.ForeColor = System.Drawing.Color.Crimson;
            this.label3.Location = new System.Drawing.Point(558, 527);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(144, 18);
            this.label3.TabIndex = 3;
            this.label3.Text = "Gönderilecek mesaj:";
            // 
            // textBoxP2Public
            // 
            this.textBoxP2Public.Location = new System.Drawing.Point(558, 82);
            this.textBoxP2Public.Multiline = true;
            this.textBoxP2Public.Name = "textBoxP2Public";
            this.textBoxP2Public.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.textBoxP2Public.Size = new System.Drawing.Size(418, 126);
            this.textBoxP2Public.TabIndex = 6;
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(555, 61);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(177, 18);
            this.label6.TabIndex = 5;
            this.label6.Text = "Açık Anahtar (Public Key):";
            // 
            // person2SendText
            // 
            this.person2SendText.Location = new System.Drawing.Point(561, 548);
            this.person2SendText.Multiline = true;
            this.person2SendText.Name = "person2SendText";
            this.person2SendText.Size = new System.Drawing.Size(418, 55);
            this.person2SendText.TabIndex = 2;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.ForeColor = System.Drawing.Color.Green;
            this.label4.Location = new System.Drawing.Point(558, 446);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(95, 18);
            this.label4.TabIndex = 1;
            this.label4.Text = "Gelen Mesaj:";
            // 
            // person2ReceivedText
            // 
            this.person2ReceivedText.BackColor = System.Drawing.SystemColors.Control;
            this.person2ReceivedText.Location = new System.Drawing.Point(561, 467);
            this.person2ReceivedText.Multiline = true;
            this.person2ReceivedText.Name = "person2ReceivedText";
            this.person2ReceivedText.Size = new System.Drawing.Size(418, 55);
            this.person2ReceivedText.TabIndex = 0;
            // 
            // textBoxCommonKey
            // 
            this.textBoxCommonKey.Location = new System.Drawing.Point(352, 264);
            this.textBoxCommonKey.Name = "textBoxCommonKey";
            this.textBoxCommonKey.Size = new System.Drawing.Size(627, 24);
            this.textBoxCommonKey.TabIndex = 10;
            // 
            // label8
            // 
            this.label8.AutoSize = true;
            this.label8.Location = new System.Drawing.Point(143, 267);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(203, 18);
            this.label8.TabIndex = 9;
            this.label8.Text = "Ortak anahtar (common key):";
            // 
            // textBoxIV
            // 
            this.textBoxIV.Location = new System.Drawing.Point(352, 297);
            this.textBoxIV.Name = "textBoxIV";
            this.textBoxIV.Size = new System.Drawing.Size(627, 24);
            this.textBoxIV.TabIndex = 16;
            // 
            // label11
            // 
            this.label11.AutoSize = true;
            this.label11.Location = new System.Drawing.Point(29, 300);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(317, 18);
            this.label11.TabIndex = 15;
            this.label11.Text = "AES başlatma vektörü (initialization vector - IV):";
            // 
            // label7
            // 
            this.label7.BackColor = System.Drawing.Color.Orange;
            this.label7.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label7.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
            this.label7.Location = new System.Drawing.Point(26, 16);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(418, 25);
            this.label7.TabIndex = 17;
            this.label7.Text = "1. Kişi";
            this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label12
            // 
            this.label12.BackColor = System.Drawing.Color.Orange;
            this.label12.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label12.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
            this.label12.Location = new System.Drawing.Point(555, 16);
            this.label12.Name = "label12";
            this.label12.Size = new System.Drawing.Size(421, 25);
            this.label12.TabIndex = 18;
            this.label12.Text = "2. Kişi";
            this.label12.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label13
            // 
            this.label13.BackColor = System.Drawing.Color.Orange;
            this.label13.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label13.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
            this.label13.Location = new System.Drawing.Point(29, 232);
            this.label13.Name = "label13";
            this.label13.Size = new System.Drawing.Size(950, 25);
            this.label13.TabIndex = 19;
            this.label13.Text = "ORTAK";
            this.label13.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.Color.Green;
            this.panel1.Location = new System.Drawing.Point(451, 578);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(70, 3);
            this.panel1.TabIndex = 20;
            // 
            // panel2
            // 
            this.panel2.BackColor = System.Drawing.Color.Green;
            this.panel2.Location = new System.Drawing.Point(518, 403);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(3, 175);
            this.panel2.TabIndex = 21;
            // 
            // panel3
            // 
            this.panel3.BackColor = System.Drawing.Color.Green;
            this.panel3.Location = new System.Drawing.Point(520, 403);
            this.panel3.Name = "panel3";
            this.panel3.Size = new System.Drawing.Size(40, 3);
            this.panel3.TabIndex = 21;
            // 
            // panel4
            // 
            this.panel4.BackColor = System.Drawing.Color.Green;
            this.panel4.Location = new System.Drawing.Point(521, 483);
            this.panel4.Name = "panel4";
            this.panel4.Size = new System.Drawing.Size(40, 3);
            this.panel4.TabIndex = 22;
            // 
            // panel5
            // 
            this.panel5.BackColor = System.Drawing.Color.Crimson;
            this.panel5.Location = new System.Drawing.Point(450, 500);
            this.panel5.Name = "panel5";
            this.panel5.Size = new System.Drawing.Size(50, 3);
            this.panel5.TabIndex = 24;
            // 
            // panel6
            // 
            this.panel6.BackColor = System.Drawing.Color.Crimson;
            this.panel6.Location = new System.Drawing.Point(450, 419);
            this.panel6.Name = "panel6";
            this.panel6.Size = new System.Drawing.Size(50, 3);
            this.panel6.TabIndex = 23;
            // 
            // panel7
            // 
            this.panel7.BackColor = System.Drawing.Color.Crimson;
            this.panel7.Location = new System.Drawing.Point(497, 559);
            this.panel7.Name = "panel7";
            this.panel7.Size = new System.Drawing.Size(65, 3);
            this.panel7.TabIndex = 25;
            // 
            // panel8
            // 
            this.panel8.BackColor = System.Drawing.Color.Crimson;
            this.panel8.Location = new System.Drawing.Point(497, 419);
            this.panel8.Name = "panel8";
            this.panel8.Size = new System.Drawing.Size(3, 140);
            this.panel8.TabIndex = 24;
            // 
            // label14
            // 
            this.label14.AutoSize = true;
            this.label14.Location = new System.Drawing.Point(463, 490);
            this.label14.Name = "label14";
            this.label14.Size = new System.Drawing.Size(23, 18);
            this.label14.TabIndex = 26;
            this.label14.Text = "
Yorum Yaz

Yorum yazabilmek için üye girişi yapmanız gerekiyor!

Yukarı Git