Click here to Skip to main content
15,886,714 members
Articles / Programming Languages / C#

Elliptic Curve Diffie Hellman Cryptography

Rate me:
Please Sign up or sign in to vote.
4.23/5 (20 votes)
30 Apr 200712 min read 145.8K   4.4K   40  
Protect yourself from terrorism with Vista's new Crypto API: This article shows you how to add NSA-level encryption to your programs. Warning: the included code samples are classified as munitions, and may not be exported to designated nations under arms control laws.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DiffieHellmanMerkle;
using System.Security.Cryptography;
using System.IO;

namespace TestEllipticCurveDiffieHellman
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            byte[] SecretA=null;
            byte[] SecretB=null;
            try
            {
                ECDiffieHellmanMerkle A = new ECDiffieHellmanMerkle(ECDHAlgorithm.ECDH_384);
                ECDiffieHellmanMerkle B = new ECDiffieHellmanMerkle(ECDHAlgorithm.ECDH_384);
                A.KeyDerivationFunction = ECDHKeyDerivationFunction.HASH;
                B.KeyDerivationFunction = ECDHKeyDerivationFunction.HASH;
                A.HashAlgorithm = DerivedKeyHashAlgorithm.SHA256_ALGORITHM;
                B.HashAlgorithm = DerivedKeyHashAlgorithm.SHA256_ALGORITHM;
                SecretA = A.RetrieveSecretKey(B.PublicKey);
                SecretB = B.RetrieveSecretKey(A.PublicKey);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"Win32 Error Message");
            }

            //A encrypts the message with her secret key
            string SecretMessage = "The owl of Minerva only flies at dusk.";
            byte[] SecretMessageByteArray = Encoding.Unicode.GetBytes(SecretMessage);
            string IVString = "initialV";
            byte[] IVByteArray = Encoding.Unicode.GetBytes(IVString);
            RijndaelManaged rijndael = new RijndaelManaged();
            ICryptoTransform encryptor = rijndael.CreateEncryptor(SecretA, IVByteArray);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
            cryptoStream.Write(SecretMessageByteArray, 0, SecretMessageByteArray.Length);
            cryptoStream.FlushFinalBlock();
            byte[] cipherText = memoryStream.ToArray();
            memoryStream.Close();
            cryptoStream.Close();

            //B decrypts the message with his secret key
            ICryptoTransform decryptor = rijndael.CreateDecryptor(SecretB, IVByteArray);
            memoryStream = new MemoryStream(cipherText);
            cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
            byte[] clearText = new byte[cipherText.Length];
            int clearTextByteSize = cryptoStream.Read(clearText, 0, clearText.Length);
            memoryStream.Close();
            cryptoStream.Close();
            this.textBox1.Text = Encoding.Unicode.GetString(clearText, 0, clearTextByteSize);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
James is a program writer for a respectable software company. He is also a Microsoft MVP.

Comments and Discussions