Skip to content

Commit

Permalink
Updates to DiffieHellman
Browse files Browse the repository at this point in the history
  • Loading branch information
sjh37 committed Apr 11, 2018
1 parent 9d09258 commit c31e74e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 25 deletions.
34 changes: 13 additions & 21 deletions DiffieHellman.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public sealed class DiffieHellman
private readonly Aes _aes;
private readonly ECDiffieHellmanCng _diffieHellman;
public byte[] PublicKey { get; }
public byte[] IV => _aes.IV;

public DiffieHellman()
{
Expand All @@ -25,37 +24,30 @@ public DiffieHellman()
PublicKey = _diffieHellman.PublicKey.ToByteArray();
}

public byte[] Encrypt(byte[] publicKey, string secretMessage)
public byte[] Encrypt(DiffieHellman otherPerson, string secretMessage)
{
byte[] encryptedMessage;
var key = CngKey.Import(publicKey, CngKeyBlobFormat.EccPublicBlob);
var derivedKey = _diffieHellman.DeriveKeyMaterial(key); // "Common secret"

_aes.Key = derivedKey;
// Common secret created by Diffie Hellman
_aes.Key = _diffieHellman.DeriveKeyMaterial(CngKey.Import(otherPerson.PublicKey, CngKeyBlobFormat.EccPublicBlob));

using (var cipherText = new MemoryStream())
{
using (var cs = new CryptoStream(cipherText, _aes.CreateEncryptor(), CryptoStreamMode.Write))
{
var ciphertextMessage = Encoding.UTF8.GetBytes(secretMessage);
var ciphertextMessage = Encoding.Unicode.GetBytes(secretMessage);
cs.Write(ciphertextMessage, 0, ciphertextMessage.Length);
cs.Close();
}

encryptedMessage = cipherText.ToArray();
return cipherText.ToArray();
}

return encryptedMessage;
}

public string Decrypt(byte[] publicKey, byte[] encryptedMessage, byte[] iv)
public string Decrypt(DiffieHellman otherPerson, byte[] encryptedMessage)
{
string decryptedMessage;
var key = CngKey.Import(publicKey, CngKeyBlobFormat.EccPublicBlob);
var derivedKey = _diffieHellman.DeriveKeyMaterial(key);

_aes.Key = derivedKey;
_aes.IV = iv;
// Common secret created by Diffie Hellman
_aes.Key = _diffieHellman.DeriveKeyMaterial(CngKey.Import(otherPerson.PublicKey, CngKeyBlobFormat.EccPublicBlob));
var backupIV = _aes.IV;
_aes.IV = otherPerson._aes.IV;

using (var plainText = new MemoryStream())
{
Expand All @@ -65,10 +57,10 @@ public string Decrypt(byte[] publicKey, byte[] encryptedMessage, byte[] iv)
cs.Close();
}

decryptedMessage = Encoding.UTF8.GetString(plainText.ToArray());
}
_aes.IV = backupIV;

return decryptedMessage;
return Encoding.Unicode.GetString(plainText.ToArray());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,41 @@ public class DiffieHellmanTests
[Test]
public void Encrypt_Decrypt()
{
var text = "Hello World!";
const string text = "Hello World!";

var bob = new DiffieHellman();
var alice = new DiffieHellman();
var bob = new DiffieHellman();

// Bob uses Alice's public key to encrypt his message.
var secretMessage = bob.Encrypt(alice.PublicKey, text);
var secretMessage = bob.Encrypt(alice, text);

// Alice uses Bob's public key and IV to decrypt the secret message.
var decryptedMessage = alice.Decrypt(bob.PublicKey, secretMessage, bob.IV);
var decryptedMessage = alice.Decrypt(bob, secretMessage);
Assert.AreEqual(text, decryptedMessage);
}

[Test]
public void MultipleTests()
{
const string text = "Hello World!";

var alice = new DiffieHellman();
var bob = new DiffieHellman();

var secretMessageA = alice.Encrypt(bob, text);
var secretMessage1 = bob.Encrypt(alice, text);
var decryptedMessage = alice.Decrypt(bob, secretMessage1);
var secretMessageB = alice.Encrypt(bob, text);
Assert.AreEqual(text, decryptedMessage);
Assert.AreEqual(secretMessageA, secretMessageB);

// See if its repeatable due to IV being replaced by previous decryption
var secretMessage2 = bob.Encrypt(alice, text);
decryptedMessage = alice.Decrypt(bob, secretMessage2);
Assert.AreEqual(text, decryptedMessage);

// Should be the same if nothing has changed
Assert.AreEqual(secretMessage1, secretMessage2);
}
}
}
1 change: 1 addition & 0 deletions Effortless.Net.Encryption.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="Strings.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<None Include="Effortless.Net.Encryption.pfx" />
<None Include="README.md" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,13 @@ ds.AssignNewKey();
var signature = ds.SignData(hash);
var result = ds.VerifySignature(hash, signature);
Assert.IsTrue(result);

// Diffie Hellman
var alice = new DiffieHellman();
var bob = new DiffieHellman();
// Bob uses Alice's public key to encrypt his message.
var secretMessage = bob.Encrypt(alice, "Hello");
// Alice uses Bob's public key and IV to decrypt the secret message.
var decryptedMessage = alice.Decrypt(bob, secretMessage);
Assert.AreEqual("Hello", decryptedMessage);
```

0 comments on commit c31e74e

Please sign in to comment.