finished standard authenticated pubkey encryption, but no decryption yet

This commit is contained in:
Kevin Froman 2020-05-09 05:26:38 -05:00
parent 8f35b51074
commit bbeaa837a9
5 changed files with 86 additions and 8 deletions

52
tests/Curve25519Test.cs Normal file
View File

@ -0,0 +1,52 @@
using NUnit.Framework;
using chestcrypto;
using System;
using System.Text;
using Sodium;
namespace Curve25519Tests
{
public class Tests
{
[SetUp]
public void Setup()
{
}
[Test]
public void TestCurve25519Encrypt()
{
var alice = PublicKeyBox.GenerateKeyPair();
var bob = PublicKeyBox.GenerateKeyPair();
string message = "Hello World";
byte[] message_bytes = UTF8Encoding.UTF8.GetBytes(message);
byte[] encrypted_with_nonce = chestcrypto.Curve25519.encrypt(alice.PrivateKey, bob.PublicKey, message_bytes);
byte[] used_nonce = new byte[24];
byte[] encrypted_without_nonce = new byte[encrypted_with_nonce.Length - 24];
int counter = 0;
Console.WriteLine(encrypted_without_nonce.Length);
for (int i = 24; i < encrypted_with_nonce.Length; i++){
//Console.WriteLine(counter);
encrypted_without_nonce[counter] = encrypted_with_nonce[i];
counter += 1;
}
for (int i = 0; i < chestcrypto.Curve25519.NONCE_BYTE_AMOUNT; i++){
//Console.WriteLine(i);
used_nonce[i] = encrypted_with_nonce[i];
}
for (int i = 0; i < chestcrypto.Curve25519.NONCE_BYTE_AMOUNT; i++){
if (used_nonce[i] != encrypted_with_nonce[i]){
Assert.Fail();
}
}
byte[] decrypted = PublicKeyBox.Open(encrypted_without_nonce, used_nonce, bob.PrivateKey, alice.PublicKey);
if (!Encoding.UTF8.GetString(decrypted, 0, decrypted.Length).Equals(message)){
Console.WriteLine(Encoding.UTF8.GetString(decrypted, 0, decrypted.Length));
Assert.Fail();
}
}
}
}

View File

@ -23,5 +23,6 @@ namespace tests
}
Assert.Pass();
}
}
}

View File

@ -0,0 +1,13 @@
using System;
namespace chestcrypto{
internal class ByteCombiner{
internal static byte[] Combine(byte[] first, byte[] second)
{
byte[] bytes = new byte[first.Length + second.Length];
Buffer.BlockCopy(first, 0, bytes, 0, first.Length);
Buffer.BlockCopy(second, 0, bytes, first.Length, second.Length);
return bytes;
}
}
}

View File

@ -0,0 +1,19 @@
using Sodium;
namespace chestcrypto {
public class Curve25519{
public static int NONCE_BYTE_AMOUNT = 24;
public static byte[] encrypt(byte[] privkey, byte[] pubkey, byte[] message){
byte[] nonce = Sodium.PublicKeyBox.GenerateNonce();
return ByteCombiner.Combine
(nonce,
Sodium.PublicKeyBox.Create(
message,
nonce,
privkey,
pubkey
));
}
}
}

View File

@ -21,18 +21,11 @@ internal class Curve25519KeyGenerator{
namespace chestcrypto{
public class PrivateKeyGenerator{
private static byte[] Combine(byte[] first, byte[] second)
{
byte[] bytes = new byte[first.Length + second.Length];
Buffer.BlockCopy(first, 0, bytes, 0, first.Length);
Buffer.BlockCopy(second, 0, bytes, first.Length, second.Length);
return bytes;
}
public static byte[] generate()
{
byte[] ed25519 = Ed25519KeyGenerator.generator();
byte[] curve25519 = Curve25519KeyGenerator.generator();
byte[] key = Combine(ed25519, curve25519);
byte[] key = chestcrypto.ByteCombiner.Combine(ed25519, curve25519);
Array.Clear(ed25519, 0, ed25519.Length);
Array.Clear(curve25519, 0, curve25519.Length);
return key;