diff --git a/tests/Curve25519Test.cs b/tests/Curve25519Test.cs index 8c1cc09..91aa465 100644 --- a/tests/Curve25519Test.cs +++ b/tests/Curve25519Test.cs @@ -13,6 +13,32 @@ namespace Curve25519Tests { } + [Test] + public void TestCurve25519Decrypt() + { + var alice = PublicKeyBox.GenerateKeyPair(); + var bob = PublicKeyBox.GenerateKeyPair(); + string message = "Hello World"; + byte[] message_bytes = UTF8Encoding.UTF8.GetBytes(message); + byte[] nonce = Sodium.PublicKeyBox.GenerateNonce(); + + byte[] encrypted = + Sodium.PublicKeyBox.Create( + message, + nonce, + alice.PrivateKey, + bob.PublicKey + ); + byte[] both = new byte[nonce.Length + encrypted.Length]; + Buffer.BlockCopy(nonce, 0, both, 0, nonce.Length); + Buffer.BlockCopy(encrypted, 0, both, nonce.Length, encrypted.Length); + byte[] decrypted = chestcrypto.Curve25519.decrypt(bob.PrivateKey, alice.PublicKey, both); + string decrypted_string = Encoding.UTF8.GetString(decrypted, 0, decrypted.Length); + if (! decrypted_string.Equals(message)){ + Assert.Fail(); + } + } + [Test] public void TestCurve25519Encrypt() { @@ -26,15 +52,13 @@ namespace Curve25519Tests 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++){ @@ -44,7 +68,6 @@ namespace Curve25519Tests } 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(); } } diff --git a/treasurechest/chestcrypto/curve25519.cs b/treasurechest/chestcrypto/curve25519.cs index 83637a9..59c0698 100644 --- a/treasurechest/chestcrypto/curve25519.cs +++ b/treasurechest/chestcrypto/curve25519.cs @@ -5,6 +5,7 @@ namespace chestcrypto { public static int NONCE_BYTE_AMOUNT = 24; public static byte[] encrypt(byte[] privkey, byte[] pubkey, byte[] message){ + // Take a byte message and priv/pubkey for authenticated encryption and return encrypted data with prepended nonce byte[] nonce = Sodium.PublicKeyBox.GenerateNonce(); return ByteCombiner.Combine (nonce, @@ -15,5 +16,21 @@ namespace chestcrypto { pubkey )); } + + public static byte[] decrypt(byte[] privkey, byte[] pubkey, byte[] message){ + byte[] nonce = new byte[NONCE_BYTE_AMOUNT]; + byte[] encrypted = new byte[message.Length - NONCE_BYTE_AMOUNT]; + int counter = 0; + for (int i = 0; i < message.Length; i++){ + if (i < NONCE_BYTE_AMOUNT){ + nonce[i] = message[i]; + continue; + } + encrypted[counter] = message[i]; + counter += 1; + } + + return Sodium.PublicKeyBox.Open(encrypted, nonce, privkey, pubkey); + } } } \ No newline at end of file