⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 椭圆曲线上的Diffie_hellman的C#实现
💻 CS
字号:
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);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -