Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Created January 4, 2017 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/6f28f05b47ebf7f87f6d40aba84c7fee to your computer and use it in GitHub Desktop.
Save anonymous/6f28f05b47ebf7f87f6d40aba84c7fee to your computer and use it in GitHub Desktop.
EccDiffHellman
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace encryptMo
{
class ExchangeKeyRequest
{
public string MyPublicKey { get; set; }
}
class ExchangeKeyResponse
{
public string MyPublicKey { get; set; }
}
class AliceRequest
{
public string EncryptAliceReq1 { get; set; }
public string EncryptAliceReq2 { get; set; }
public string ExtraPublicParam1 { get; set; }
public string ExtraPublicParam2 { get; set; }
}
class BobRequest
{
public string EncryptBobReq1 { get; set; }
public string EncryptBobReq2 { get; set; }
public string ExtraPublicParam1 { get; set; }
public string ExtraPublicParam2 { get; set; }
}
class AliceResponse
{
public string EncryptAliceRep1 { get; set; }
public string ExtraPublicParam1 { get; set; }
}
class BobResponse
{
public string EncryptBobRep1 { get; set; }
public string ExtraPublicParam1 { get; set; }
}
class ServerPrepare
{
private byte[] publicKey;
private ECDiffieHellmanCng exchange;
public ServerPrepare()
{
exchange = new ECDiffieHellmanCng();
exchange.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
exchange.HashAlgorithm = CngAlgorithm.Sha256;
publicKey = exchange.PublicKey.ToByteArray();
}
public byte[] GetEncryptKey(byte[] otherpublicKey)
{
return exchange.DeriveKeyMaterial(CngKey.Import(otherpublicKey, CngKeyBlobFormat.EccPublicBlob));
}
public byte[] PublicKey => publicKey;
public byte[] YourPublicKey { get; set; }
}
class ServerEncrypt
{
public static string Encrypt(string text, byte[] key, ref byte[] iv)
{
using (Aes aes = new AesCryptoServiceProvider())
{
aes.Key = key;
if (iv == null)
{
iv = aes.IV;
}
else
{
aes.IV = iv;
}
using (MemoryStream ciphertext = new MemoryStream())
using (CryptoStream cs = new CryptoStream(ciphertext, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] plaintextMessage = Encoding.UTF32.GetBytes(text);
cs.Write(plaintextMessage, 0, plaintextMessage.Length);
cs.Close();
var encryptedMessage = ciphertext.ToArray();
return Convert.ToBase64String(encryptedMessage);
}
}
}
public static string Encrypt2(string text, byte[] key, ref byte[] iv)
{
using (Aes aes = new AesCryptoServiceProvider())
{
aes.Key = key;
if (iv == null)
{
iv = aes.IV;
}
else
{
aes.IV = iv;
}
using (MemoryStream ciphertext = new MemoryStream())
using (CryptoStream cs = new CryptoStream(ciphertext, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(cs))
{
writer.Write(text);
writer.Flush();
}
var encryptedMessage = ciphertext.ToArray();
return Convert.ToBase64String(encryptedMessage);
}
}
}
public static string Decrypt(string chaos, byte[] key, byte[] iv)
{
using (Aes aes = new AesCryptoServiceProvider())
{
aes.Key = key;
aes.IV = iv;
var encryptedMessage = Convert.FromBase64String(chaos);
using (MemoryStream plaintext = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(plaintext, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(encryptedMessage, 0, encryptedMessage.Length);
cs.Close();
string message = Encoding.UTF32.GetString(plaintext.ToArray());
return message;
}
}
}
}
public static string Decrypt2(string chaos, byte[] key, byte[] iv)
{
using (Aes aes = new AesCryptoServiceProvider())
{
aes.Key = key;
aes.IV = iv;
var encryptedMessage = Convert.FromBase64String(chaos);
// Decrypt the message
using (MemoryStream plaintext = new MemoryStream(encryptedMessage))
{
using (CryptoStream cs = new CryptoStream(plaintext, aes.CreateDecryptor(), CryptoStreamMode.Read))
{
using (StreamReader reader = new StreamReader(cs))
{
return reader.ReadToEnd();
}
}
}
}
}
}
class ExchangeServer
{
internal ServerPrepare Prepare = new ServerPrepare();
public ExchangeKeyResponse Exchange(ExchangeKeyRequest request)
{
Prepare.YourPublicKey = Convert.FromBase64String(request.MyPublicKey);
return new ExchangeKeyResponse() { MyPublicKey = Convert.ToBase64String(Prepare.PublicKey) };
}
}
class AliceServer : ExchangeServer
{
public AliceRequest SayHi(string message)
{
var request = new AliceRequest()
{
EncryptAliceReq1 = message,
EncryptAliceReq2 = "from alice",
};
var encryptKey = Prepare.GetEncryptKey(Prepare.YourPublicKey);
byte[] iv = null;
request.EncryptAliceReq1 = ServerEncrypt.Encrypt(request.EncryptAliceReq1, encryptKey, ref iv);
request.EncryptAliceReq2 = ServerEncrypt.Encrypt(request.EncryptAliceReq2, encryptKey, ref iv);
request.ExtraPublicParam1 = Convert.ToBase64String(iv);
Console.WriteLine("alice-encryptkey: {0}", byteconvert.Convert(encryptKey));
Console.WriteLine("alice-iv: {0}", byteconvert.Convert(iv));
Console.WriteLine("alice-encryptReq1: {0}", request.EncryptAliceReq1);
return request;
}
public string WhatIsBack(BobResponse response)
{
var encryptKey = Prepare.GetEncryptKey(Prepare.YourPublicKey);
byte[] iv = Convert.FromBase64String(response.ExtraPublicParam1);
var message = ServerEncrypt.Decrypt(response.EncryptBobRep1, encryptKey, iv);
return message;
}
}
class BobServer : ExchangeServer
{
public BobResponse ReplyHi(AliceRequest aliceRequest)
{
BobResponse response = new BobResponse();
var encryptKey = Prepare.GetEncryptKey(Prepare.YourPublicKey);
byte[] iv = Convert.FromBase64String(aliceRequest.ExtraPublicParam1);
var message = ServerEncrypt.Decrypt(aliceRequest.EncryptAliceReq1, encryptKey, iv);
response.EncryptBobRep1 = ServerEncrypt.Encrypt(message + " too", encryptKey, ref iv);
response.ExtraPublicParam1 = aliceRequest.ExtraPublicParam1;
Console.WriteLine("bob-encryptkey: {0}", byteconvert.Convert(encryptKey));
Console.WriteLine("bob-iv: {0}", byteconvert.Convert(iv));
Console.WriteLine("bob-message: {0}", message);
return response;
}
}
class AliceAndBob
{
public static void TellAStory(string text)
{
AliceServer alice = new AliceServer();
BobServer bob = new BobServer();
alice.Exchange(new ExchangeKeyRequest { MyPublicKey = Convert.ToBase64String(bob.Prepare.PublicKey) });
bob.Exchange(new ExchangeKeyRequest { MyPublicKey = Convert.ToBase64String(alice.Prepare.PublicKey) });
var request = alice.SayHi(text);
var response = bob.ReplyHi(request);
var backMessage = alice.WhatIsBack(response);
Console.WriteLine(backMessage);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment